I am working on a script that can generate a shortcut for each Powershell script present in a particular directory with administrator privileges. Here is my progress 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 code is able to generate a shortcut with administrative privileges for each script, but the target of the shortcut is only set to Powershell and not to the actual scripts.
It is important for me to set the shortcut target to “Powershell -f [script path]”, as shown in this example:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f "C:\Users\GJBalaich\Google Drive\Powershell Scripts\FFmpeg\FFclip.ps1"
However, when I attempt to define the target path using Powershell, here are some 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 raises an error message “Value does not fall within the expected range” when attempting to set the target path using Powershell. Do you have any suggestions or insights on how to fix this issue?
3 Answers
Introduction
Creating shortcuts for frequently used scripts is a great way to save time and increase productivity. However, when working with PowerShell scripts, it is important to ensure that the shortcut target path includes both the application and the script file. In this blog post, we will discuss how to provide a target path to the shortcut that includes both the application and a file.
Understanding the Issue
When generating a shortcut using PowerShell, it is important to set the target path correctly. The target path should include the path to the PowerShell executable, along with the path to the script file. For example, the target path for a PowerShell script located at “C:ScriptsMyScript.ps1” should be set to “C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -File C:ScriptsMyScript.ps1”.
The issue with the code provided in the introduction is that it sets the target path to only the PowerShell executable and does not include the path to the script file. This means that the shortcut will open PowerShell, but it will not run the script file.
Solution
To provide a target path to the shortcut that includes both the application and a file, we need to modify the code provided in the introduction. Here is an updated version of the code that sets the target path correctly:
$scripts = Get-ChildItem -path "C:UsersdjcimGoogle DrivePowershell 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:WindowsSystem32WindowsPowerShellv1.0powershell.exe"
$Shortcut.Arguments = "-File <code>"$($script.FullName)
""
$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:UsersdjcimGoogle DrivePowershell ScriptsAdmin Shortcuts" -force
}
Let’s take a closer look at the changes made to the code.
Setting the target path
The first change we need to make is to set the target path correctly. In the original code, the target path was set to the PowerShell executable only. To set the target path to include the script file, we need to set the “TargetPath” property of the shortcut to the path of the PowerShell executable, and the “Arguments” property to the path of the script file.
Here is the updated code that sets the target path correctly:
$Shortcut.TargetPath = "C:WindowsSystem32WindowsPowerShellv1.0powershell.exe"
$Shortcut.Arguments = "-File <code>"$($script.FullName)
""
The “Arguments” property is set to “-File "$($script.FullName)
“”, which tells PowerShell to run the script file located at the path specified in the “FullName” property of the script object.
Saving the shortcut
Once we have set the target path correctly, we need to save the shortcut. This is done using the “Save” method of the shortcut object.
Here is the updated code that saves the shortcut:
$Shortcut.Save()
Setting administrative privileges
The original code set administrative privileges for the generated shortcuts by modifying the shortcut file directly. However, this is not the recommended way to set administrative privileges for a shortcut.
Instead, we can set administrative privileges for the PowerShell process that runs the script. This is done by adding the “-Verb RunAs” argument to the “Arguments” property of the shortcut object.
Here is the updated code that sets administrative privileges for the PowerShell process:
$Shortcut.Arguments = "-File <code>"$($script.FullName)
" -Verb RunAs"
Moving the shortcut
Finally, we need to move the generated shortcut to a different directory. This is done using the “Move-Item” cmdlet.
Here is the updated code that moves the generated shortcut:
Move-Item -Path $shortcutFile -Destination "C:UsersdjcimGoogle DrivePowershell ScriptsAdmin Shortcuts" -force
Conclusion
Generating shortcuts for PowerShell scripts can save a lot of time and increase productivity. However, it is important to ensure that the shortcut target path includes both the application and the script file. In this blog post, we discussed how to provide a target path to the shortcut that includes both the application and a file. We also looked at the changes that need to be made to the code provided in the introduction to set the target path correctly. By following these steps, you can generate shortcuts for your PowerShell scripts that include the script file and run with administrative privileges.
To set the target path of the shortcut to include both the application (PowerShell) and a file, you can use the following syntax:
$Shortcut.TargetPath = ‘”C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” -f “‘ + $script.FullName + ‘”‘
This will set the target path to be the full path to the PowerShell executable, followed by the -f
flag and the full path to the script file, all surrounded by quotation marks. The quotation marks are necessary to ensure that the full paths with spaces in them are interpreted correctly by the operating system.
Here is the modified script that incorporates this change:
$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" -f "' + $script.FullName + '"'
$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
}
I hope this helps! Let me know if you have any further questions.
To pass arguments to the target, follow these instructions:
'Set the additional parameters for the shortcut
$Shortcut.Arguments = "C:\Users\GJBalaich\Google Drive\Powershell Scripts\FFmpeg\FFclip.ps1"