How can I create a desktop shortcut that runs PowerShell device commands in sequence?
Asked by
!piston travel+
I want one desktop shortcut to run three PowerShell commands sequentially: enable my GPU device, wait for that operation to finish, disable it, and then enable it again. I already have separate shortcuts that work independently, but I'm not sure how to combine them into one reliable sequence. The device instance ID is `PCIVEN_1002&DEV_73DF&SUBSYS_52101849&REV_C56&399090B&0&00000019`. The commands use `Enable-PnpDevice` and `Disable-PnpDevice` with confirmation disabled. This is a workaround for recurring GPU crashes, so the shortcut may need to run with administrator privileges.
Answers
You can also place the commands in a `.bat` file and call PowerShell once, but a `.ps1` script is cleaner and makes the order explicit. If you use a batch file, the important part is to invoke PowerShell with a single command string and separate the commands with semicolons. Make sure the batch file or its shortcut is launched as administrator; otherwise the enable and disable operations may fail silently or return an access-denied error.
Reply to this answer
Put the commands in a PowerShell script rather than trying to chain separate shortcut targets. Save this as something like `Reset-Gpu.ps1` and run the shortcut as administrator:
```powershell
$deviceId = 'PCIVEN_1002&DEV_73DF&SUBSYS_52101849&REV_C56&399090B&0&00000019'
Enable-PnpDevice -InstanceId $deviceId -Confirm:$false
Start-Sleep -Seconds 2
Disable-PnpDevice -InstanceId $deviceId -Confirm:$false
Start-Sleep -Seconds 2
Enable-PnpDevice -InstanceId $deviceId -Confirm:$false
```
Set the shortcut target to:
```text
C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy Bypass -File "C:PathToReset-Gpu.ps1"
```
Open the shortcutโs Properties, choose Advanced, and enable โRun as administrator.โ The device-management cmdlets generally require elevation.
~Deadline extended+: Adding a short delay between operations is useful here. Without it, Windows may still be processing the first device state change when the next command starts.
Reply to this answer
Add your answer
Answers are reviewed before publication.
!piston travel+: That explains why my individual shortcuts worked inconsistently. Iโll use the script approach and run the shortcut elevated.