I am creating a basic powershell script that will open the calculator app, get its process id, and then eventually stop the process itself with confirmation. But when I run the following script on my computer (through Powershell ISE, but it also doesn't seem to work on regular powershell in admin mode), it does not kill the calculator process. I am running Windows 10 Version 21H1.
Start-Process calc.exe; Get-Process calc; Stop-Process -Name calc -whatif; Stop-Process -Name calc -Confirm -PassThru
I had to use the name Calculator instead of calc for Get-Process. I also had to add a delay between the launching of calc.exe and Stop-Process. Without the delay, it could not find the process to stop it.
Start-Process -FilePath calc.exe
Start-Sleep -Seconds 3
Get-Process -Name Calculator | Stop-Process
Related
I want to run this powershell script with admin privileges so it won't give me the error:
Script Output with error
Here is the script:
$processes = "PDStyleAgent", "AdobeIPCBroker", "CCXProcess", "RichVideo64", "CCLibrary", "AdobeNotificationClient", "AdobeUpdateService", "PDHanumanSvr", "PDR"
Foreach ($process in $processes){
try {
$f = Get-Process $process -ErrorAction Stop
$f | kill
Write-Output "$process killed."
}
catch [Microsoft.PowerShell.Commands.ProcessCommandException]{
Write-Output "No instances of $process running."
}
}
Start-Sleep -Seconds 3
I want to run this script so it kill the processes that are giving errors
Some way to run PowerShell with admin privileges:
Search Powershell from the Windows search icon OR click the Windows button from the keyboard --> write Powershell --> You will see the Windows PowerShell --> Right-click on Powershell then click Run as administrator.
Run this command of a PowerShell console: Start-Process powershell -Verb runAs
Run your script from PowerShell like this: PowerShell -f C:\ScriptPath
For more details, you can check this StackOverflow question and answer.
I'm not sure what exactly you are asking about. If you want to start a script as administrator, you need to open PowerShell window "As administrator".
BTW, the script itself can be simplified without losing functionality:
$processes = "PDStyleAgent", "AdobeIPCBroker", "CCXProcess", "RichVideo64", "CCLibrary", "AdobeNotificationClient", "AdobeUpdateService", "PDHanumanSvr", "PDR"
Get-Process -Name $processes -ErrorAction SilentlyContinue | Stop-Process -Verbose
1.Create a shortcut to your Powershell script on your desktop
2.Right-click the shortcut and click Properties
3.Click the Shortcut tab
4.Click Advanced
5.Select Run as Administrator
NOTE: add powershell -f in front of the script path
Trying to run a chkdsk on all drives of the computer. Having issues with having C:\ start at all.
Trying to use SendKeys to answer "Would you like to schedule this volume to be checked the next time the system restarts? (Y/N)" but having no luck. Where am I going wrong?
$host.ui.RawUI.WindowTitle = "BOHCdrivefix"
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {chkdsk c:/f}
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('BOHCdrivefix')
sleep 3
$wshell.SendKeys('y')
$wshell.SendKeys('~')
wait-job $FixCDrive
Receive-Job $FixCDrive | Out-File -FilePath D:\temp\cDriveFix.txt
shutdown -r -f -t 0
I would like to answer Y to the question then shutdown the PC and have it start the chkdsk
From Start-Job document:
The Start-Job cmdlet starts a Windows PowerShell background job on
the local computer.
A Windows PowerShell background job runs a command without
interacting with the current session.
Hence, you can't send any key from the current session to a command running inside a background job. Sending a key must be inside the background job. Fortunately, chkdsk.exe accepts use pipeline operator (which sends the results of the preceding command to the next command) as follows:
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {Write-Output 'y'|chkdsk.exe c:/f}
or (using echo alias for Write-Output cmdlet):
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {echo 'y'|chkdsk.exe c:/f}
Please note:
The type of the file system is NTFS.
Cannot lock current drive.
Chkdsk cannot run because the volume is in use by another
process. Would you like to schedule this volume to be
checked the next time the system restarts? (Y/N)
To answer "yes" to above question asked by chkdsk c:/f (fix file system errors on the boot partition), you must press Y, followed by Enter.
Honestly said, I'm not sure whether Write-Output cmdlet sends Enter into the pipeline. If not, force output of the new line as follows:
Write-Output "y$([System.Environment]::NewLine)"|chkdsk.exe c:/f}
I am trying to make a service that scans a folder. When it detects certain files it renames them and then creates variables for the rest of the script. This all works fine, but I am trying to find a way to run this script so that the subsequent windows that it launches are minimized. I've tried the below, but the 2 scripts still launch a window each that flashes on the screen.
I have tried converting the file into an EXE and a service but then it refuses to work. Hoping for a quick one-liner if anyone can help?
Set-Location -Path C:\scripts\ICVT
while ($true) {
invoke-expression 'cmd /c start powershell -WindowStyle Minimized -Command {.\file_checker.ps1}';
Start-Sleep 5
invoke-expression 'cmd /c start powershell -WindowStyle Minimized -Command {.\kill_checker.ps1}';
Start-Sleep 5
}
Sounds like you need to use Start-Job to run the script in the background. Simply replace the Invoke-Expression with Start-Job:
Set-Location -Path C:\scripts\ICVT
while ($true) {
Start-Job -FilePath '.\file_checker.ps1}';
Start-Sleep 5
Start-Job -FilePath '.\kill_checker.ps1}';
Start-Sleep 5
}
It will also accept script blocks as well. See about_jobs for more information on running and managing background jobs.
I write this command for powershell. I want to stop the running process of taskmgr from PowerShell.
Get-Service | Where-Object {$_.Status -eq "Running"}
And how to stop process those running processes?
Task Manager is a process, not a service.
So, in order to stop it from PowerShell you need to run:
Stop-Process -Name Taskmgr -Force
PS: You will need to run the powershell as administrator in order to stop the process.
Best regards,
I have a VB application which generates a PDF file then runs a powershell script which prints it.
If I run the application manually (by double-clicking on it or through the DOS screen) everything works. The pdf gets generated & printed.
If I run the application through windows scheduled tasks the pdf gets generated but it doesn't get printed!
This is my powershell script:
Start-Process –FilePath $DocumentName –Verb PrintTo $PrinterName -PassThru | %{sleep 20;$_} | kill
at run time it becomes something like this:
Start-Process –FilePath E:\MyFolder\mydocument.pdf –Verb PrintTo MyPrinter -PassThru | %{sleep 30;$_} | kill
I tried changing it to this:
Start-Process –FilePath "E:\MyFolder\mydocument.pdf" –Verb PrintTo MyPrinter -PassThru | %{sleep 30;$_} | kill
but that hasn't worked either.
If paste it in powershell command line, it works fine!
Any suggestion anyone?
Thanks
System: Windows Server 2012 R2
This is my VB code for executing the powershell script:
Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
MyRunSpace.Open()
Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript(scriptText)