r/PowerShell • u/Devious018 • Sep 21 '26
Solved Need help with creating a powershell shortcut to work
Basically I am trying to create a desktop shortcut that would execute three commands one after another. So far I have created two separate desktop shortcuts that both work independently but have not been able to put them together to run one after another. One disables my gpu drivers and the other one enables it, i want to make a desktop shortcut that would enable the drivers and then after that is completed it would disable them and then finally re enable them
This is the disable line
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "Disable-PnpDevice -InstanceId 'PCI\VEN_1002&DEV_73DF&SUBSYS_52101849&REV_C5\6&399090B&0&00000019' -confirm:$false"
this is the enable line
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "Enable-PnpDevice -InstanceId 'PCI\VEN_1002&DEV_73DF&SUBSYS_52101849&REV_C5\6&399090B&0&00000019' -confirm:$false"
just need some help to create a sequential command to make as a desktop shortcut, and for anyone curious my gpu has issues where it regularly crashes this is the only fix that works
EDIT: Solved, created a .bat file with all commands written sequentially with a wait timer between them to let them run one by one. Thank you all for your help.
2
u/Mc_Loverbutt Sep 21 '26
I would put both lines into a powershell script (omit calling powershell.exe in the lines) make a new shortcut. Program: powershell.exe Arguments: -ExecutionPolicy Bypass -File "C:\Path-to-your-script"
In the script add a new line with "Pause". This pauses the script, add as many as you want for debugging.
0
u/BlackV Sep 21 '26
copy this line
paste it to a batch file
xxx.cmd(orxxx.batif you prefer)0
u/GoD0nkeys Sep 24 '26
# Reset-GPU.ps1
$ErrorActionPreference = 'Stop'
$InstanceId = 'PCI\VEN_1002&DEV_73DF&SUBSYS_52101849&REV_C5\6&399090B&0&00000019'
# -------------------------------------------------------------------------
# Elevate automatically if not already running as Administrator
# -------------------------------------------------------------------------
$CurrentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object Security.Principal.WindowsPrincipal($CurrentIdentity)
if (-not $Principal.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)) {
$Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
Start-Process `
-FilePath "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" `
-Verb RunAs `
-ArgumentList $Arguments
exit
}
function Wait-ForDeviceState {
param (
[Parameter(Mandatory)]
[ValidateSet('Enabled', 'Disabled')]
[string]$State,
[int]$TimeoutSeconds = 20
)
$Timeout = (Get-Date).AddSeconds($TimeoutSeconds)
do {
Start-Sleep -Milliseconds 500
$Device = Get-PnpDevice `
-InstanceId $InstanceId `
-ErrorAction Stop
if ($State -eq 'Disabled' -and $Device.Problem -eq 22) {
return
}
if ($State -eq 'Enabled' -and $Device.Status -eq 'OK') {
return
}
} while ((Get-Date) -lt $Timeout)
throw "GPU did not reach state '$State' within $TimeoutSeconds seconds."
}
try {
Write-Host 'GPU Reset starting...'
# Make sure GPU is enabled first
Write-Host 'Enabling GPU...'
Enable-PnpDevice `
-InstanceId $InstanceId `
-Confirm:$false `
-ErrorAction Stop
Wait-ForDeviceState -State Enabled
# Disable GPU
Write-Host 'Disabling GPU...'
Disable-PnpDevice `
-InstanceId $InstanceId `
-Confirm:$false `
-ErrorAction Stop
Wait-ForDeviceState -State Disabled
# Re-enable GPU
Write-Host 'Re-enabling GPU...'
Enable-PnpDevice `
-InstanceId $InstanceId `
-Confirm:$false `
-ErrorAction Stop
Wait-ForDeviceState -State Enabled
Write-Host 'GPU reset completed successfully.'
}
catch {
Write-Host ''
Write-Host "GPU reset FAILED: $($_.Exception.Message)"
Write-Host ''
Read-Host 'Press Enter to close'
exit 1
}
Start-Sleep -Seconds 2
exit 0