1

I'm attempting to create a script that creates a shortcut with admin privileges for every Powershell script in a given directory, what I have so far:

$scripts = Get-ChildItem -path "C:\Users\djcim\Google Drive\Powershell Scripts\*.ps1" -Recurse
foreach ($script in $scripts) {
    $shortcutFile = [io.path]::ChangeExtension($script.FullName, '.lnk')
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    $Shortcut.Save()

    $bytes = [System.IO.File]::ReadAllBytes($ShortcutFile)
    $bytes[0x15] = $bytes[0x15] -bor 0x20
    [System.IO.File]::WriteAllBytes($ShortcutFile, $bytes)

    Move-Item -Path $shortcutFile -Destination "C:\Users\djcim\Google Drive\Powershell Scripts\Admin Shortcuts" -force
}

The above successfully creates a shortcut with admin privileges per script, however the target is only to Powershell and not the scripts themselves.

I really need the shorcut target to be Powershell -f [script path], example:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f "C:\Users\GJBalaich\Google Drive\Powershell Scripts\FFmpeg\FFclip.ps1"

However when I try to set that as the target path via Powershell, examples:

$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f " + "`"" + $script + "`""
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f " + $script

it throws the error "Value does not fall within the expected range."

Any ideas?

1

To pass arguments to the target, do this:

'Set the additional parameters for the shortcut  
$Shortcut.Arguments = "C:\Users\GJBalaich\Google Drive\Powershell Scripts\FFmpeg\FFclip.ps1"  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.