How can I create a shortcut that runs PowerShell commands in sequence?

0
0
Asked By MellowCedar42 On

I want a desktop shortcut that runs three device-management commands in order: enable my GPU device, wait for that operation to finish, disable it, and then enable it again. I already have separate shortcuts that run the enable and disable commands successfully, but I cannot combine them into one sequential shortcut. The device instance ID is `PCIVEN_1002&DEV_73DF&SUBSYS_52101849&REV_C56&399090B&0&00000019`. My GPU regularly crashes, and cycling the device this way is the only workaround that currently helps.

2 Answers

Answered By QuietHarbor7 On

Put the commands in a PowerShell script instead of trying to chain separate shortcut targets. Save this as something like `Reset-GPU.ps1`:

```powershell
$id = 'PCIVEN_1002&DEV_73DF&SUBSYS_52101849&REV_C56&399090B&0&00000019'

Enable-PnpDevice -InstanceId $id -Confirm:$false
Start-Sleep -Seconds 2
Disable-PnpDevice -InstanceId $id -Confirm:$false
Start-Sleep -Seconds 2
Enable-PnpDevice -InstanceId $id -Confirm:$false
```

Then create a shortcut with `powershell.exe` as the program and this as the argument:

```text
-ExecutionPolicy Bypass -File "C:PathToReset-GPU.ps1"
```

The pauses are optional, but they give Windows time to finish each device change before the next command runs. The shortcut may need to run with administrator privileges because these cmdlets modify hardware devices.

MellowCedar42 -

That makes sense. I was trying to put the full PowerShell executable command on every line instead of putting the commands inside one script.

Answered By SilverMaple19 On

A batch file can also launch one PowerShell command, but a `.ps1` script is cleaner for this. If you use a batch file, call PowerShell once and place the commands in a single `-Command` block. Also make sure the instance ID uses normal ampersands (`&`), not the HTML text `&`, and quote the ID exactly as shown. Run the shortcut as administrator or the enable/disable operations may fail.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.