Changing a shortcut using Powershell [duplicate] - powershell

I'm trying to create a shortcut using PowerShell that opens certificates.
$shortcut = (New-Object -ComObject Wscript.Shell).Createshortcut("desktop\Certificates.lnk")
$shortcut.TargetPath = ("C:\Windows\System32\rundll32.exe cryptui.dll,CryptUIStartCertMgr")
$shortcut.IconLocation = ("%SystemRoot%\System32\SHELL32.DLL, 44")
$shortcut.Save()
What I currently have creates a shortcut with the target of...
"C:\Windows\System32\rundll32.exe cryptui.dll,CryptUIStartCertMgr"
When there the shortcut's target includes the "" it doesn't work. I've tried to remove them from the script, but then it launches the certificates gui once and creates a shortcut on the desktop targeted to this PC instead of certificates.

The target executable and its arguments must be specified separately, namely in the .TargetPath and .Arguments property, respectively.
(Whatever you assign to .TargetPath is considered just an executable file path, and if it contains spaces, it is automatically and invariably enclosed in "..." for you.)
Therefore:
$shortcut = (New-Object -ComObject Wscript.Shell).CreateShortcut('desktop\Certificates.lnk')
$shortcut.TargetPath = 'C:\Windows\System32\rundll32.exe' # Executable only
$shortcut.Arguments = 'cryptui.dll,CryptUIStartCertMgr' # Args to pass to executable.
$shortcut.IconLocation = '%SystemRoot%\System32\SHELL32.DLL, 44'
$shortcut.Save()
Note that I've removed unnecessary (...) enclosures and, for conceptual clarity, have switched from expandable (double-quoted) strings ("...") to verbatim (single-quoted) strings ('...'), given that no string expansion (interpolation) is required in your code.
That said, for full robustness you could replace 'C:\Windows\System32\rundll32.exe' with "$env:SystemRoot\System32\rundll32.exe" - for instant expansion - or '%SystemRoot%\System32\rundll32.exe' for letting .CreateShortcut() perform the expansion.[1]
[1] It seems that the resulting shortcut file (.lnk) stores both the expanded and the unexpanded value. When inspecting a shortcut file's properties in File Explorer - and surprisingly also via getting the property values of a WshShortcut object created with (New-Object -ComObject Wscript.Shell).CreateShortcut() - you only ever see the expanded values.

Related

Edit Target on *.lnk with PowerShell [duplicate]

I'm trying to create a shortcut using PowerShell that opens certificates.
$shortcut = (New-Object -ComObject Wscript.Shell).Createshortcut("desktop\Certificates.lnk")
$shortcut.TargetPath = ("C:\Windows\System32\rundll32.exe cryptui.dll,CryptUIStartCertMgr")
$shortcut.IconLocation = ("%SystemRoot%\System32\SHELL32.DLL, 44")
$shortcut.Save()
What I currently have creates a shortcut with the target of...
"C:\Windows\System32\rundll32.exe cryptui.dll,CryptUIStartCertMgr"
When there the shortcut's target includes the "" it doesn't work. I've tried to remove them from the script, but then it launches the certificates gui once and creates a shortcut on the desktop targeted to this PC instead of certificates.
The target executable and its arguments must be specified separately, namely in the .TargetPath and .Arguments property, respectively.
(Whatever you assign to .TargetPath is considered just an executable file path, and if it contains spaces, it is automatically and invariably enclosed in "..." for you.)
Therefore:
$shortcut = (New-Object -ComObject Wscript.Shell).CreateShortcut('desktop\Certificates.lnk')
$shortcut.TargetPath = 'C:\Windows\System32\rundll32.exe' # Executable only
$shortcut.Arguments = 'cryptui.dll,CryptUIStartCertMgr' # Args to pass to executable.
$shortcut.IconLocation = '%SystemRoot%\System32\SHELL32.DLL, 44'
$shortcut.Save()
Note that I've removed unnecessary (...) enclosures and, for conceptual clarity, have switched from expandable (double-quoted) strings ("...") to verbatim (single-quoted) strings ('...'), given that no string expansion (interpolation) is required in your code.
That said, for full robustness you could replace 'C:\Windows\System32\rundll32.exe' with "$env:SystemRoot\System32\rundll32.exe" - for instant expansion - or '%SystemRoot%\System32\rundll32.exe' for letting .CreateShortcut() perform the expansion.[1]
[1] It seems that the resulting shortcut file (.lnk) stores both the expanded and the unexpanded value. When inspecting a shortcut file's properties in File Explorer - and surprisingly also via getting the property values of a WshShortcut object created with (New-Object -ComObject Wscript.Shell).CreateShortcut() - you only ever see the expanded values.

Shortcut not showing TargetPath [duplicate]

This question already has answers here:
Get target of shortcut (.lnk) file with powershell
(2 answers)
Closed 4 years ago.
I am trying to get the path to the executable from a shortcut, in Windows 7 with PowerShell 5.0, if that makes a difference. My $workingTarget = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\SnippingTool.lnk' and the shortcut is there. When I open properties on the shortcut the Target property of the shortcut shows %windir%\system32\SnippingTool.exe. However,
$shortcut = $shell.CreateShortcut($workingTarget)
Write-Host "$($shortcut.TargetPath)"
shows nothing at all. When I tried using a different shortcut, with a fully resolved path without an environment variable, that Write-Host is fine.
My hope was to at least get the string and use Get-Command to find the path via the Paths environment variable, but even that is looking problematic since
Write-Host "$(Get-Command (Split-Path '%windir%\system32\SnippingTool.exe' -leaf))"
Write-Host "$(Get-Command (Split-Path 'SnippingTool.exe' -leaf))"
both just return SnippingTool.exe, not the correct full path.
Now, on the bright side
Write-Host "$([System.Environment]::ExpandEnvironmentVariables('%windir%\system32\SnippingTool.exe'))"
does expand as expected, but only if I can actually get %windir%\system32\SnippingTool.exe out of the shortcut, which is vexing me at the moment.
After a quick search on SO I found Jeffs answer which will satisfy your need.
Get target of shortcut (.lnk) file with powershell
$sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Snipping Tool.lnk').TargetPath
Results when I run this in ISE:
[Admin] C:\WINDOWS\system32\> $sh = New-Object -ComObject WScript.Shell
$target = $sh.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Snipping Tool.lnk').TargetPath
$target
C:\WINDOWS\system32\SnippingTool.exe
Running
Major Minor Build Revision
----- ----- ----- --------
5 1 15063 1206

Create " *.url " file using powershell and CMD

i was inquiring if there are options to create a url shortcut to be placed on c:\users\username\favorites to be applied to be added to the login script of the user on Active directory to create the favorites for any user
i tried searching all over the internet and i only found "MD" and "MKDir" commands
Not the most PowerShell answer but this is the widely used one with the best compatibility in versions. Much like this answer but small changes for URLs
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Favorites\Google.url")
$Shortcut.TargetPath = "http://www.google.ca"
$Shortcut.Save()
This will make a shortcut to Google in your favorites. $Home being one of PowerShell's Automatic Variables
If you happen to have PowerShell 5.0 it can now do this natively. Again refer to this answer for more information

Add mstsc to shortcut target powershell

A little background about what I'm trying to do and why. We are slowly migrating from using a local copy of office on our end users Win7 machines to publishing office through RDS 2012. With 2012 you can have the end users machine subscribe to a webfeed which puts the shortcuts to the actual RDP files in Appdata. In order to have our image pretty much mirror before RDS, I need to pin the shortcuts to the taskbar. If you pin the shortcut as it comes from the RDS Gateway server the icon on the taskbar is that of the .rdp file. If you edit the target for the shortcut and put mstsc.exe before the path to the .rdp file you can then pin the shortcut to the taskbar using those icons of the shortcut.
I have found posts on how to change the target field of shortcuts but nothing on how to add something to what is currently there. An environment variable is needed since the path to the shorts will be different for each user. Below is I have tried thus far
$Word = $env:userprofile + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\RemoteApp and Desktop Connections\Microsoft Word 2010.lnk"
$sh = New-Object -COM WScript.Shell
$targetPath = $sh.CreateShortcut($Word).TargetPath
$sh.TargetPath = "mstsc.exe" + $targetPath ## Make changes
$sh.Save() ## Save$shell = New-Object -COM WScript.Shell
One of the errors i'm getting is : Property 'Arguments' cannot be found on this object; make sure it exists and is settable.
Any help will be greatly appreciated.
Instead of using Shell COM object, how about using a .Net wrapper class? There is a great sample.
To use the VBAccelerator's wrapper in Powershell, extract the source code and compile a DLL like so,
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library /out:ShellLink.dll /r:System.Drawing.dll .\ShellLink.cs .\FileIcon.cs
This should create ShellLink.dll, which you can add as Powershell type like so,
Add-Type -path .\ShellLink.dll
And use the class by creating a new object like so,
$lnk = new-object vbAccelerator.Components.Shell.ShellLink
$lnk.Target = "C:\Windows\System32\mstsc.exe"
$lnk.Arguments = "some arguments"
$lnk.Description = "My awesome shortcut"
$lnk.Save("c:\temp\test.lnk")
Your code will prepend "mstsc.exe" to the current target path if you just add this line before your ## Make changes line:
$sh = $sh.CreateShortcut($word)
Sounds like you also want to add a space, so that your lnk file is the "connection file" argument of mstsc.exe. You can set the Arguments property like so:
function Set-RDSshortcut {
param( [parameter(mandatory=$true)]$Shortcut )
$sh = New-Object -COM WScript.Shell
$targetPath = $sh.CreateShortcut($Shortcut).TargetPath
$targetPath = "mstsc.exe"
$sh = $sh.CreateShortcut($Shortcut)
$sh.TargetPath = $targetPath
$sh.Arguments = $Shortcut
$sh.Save()
} #end function

creating a shortcut on a remote desktop, but the shortcut is created with a "file" instead of "file folder" as the "target type"

I'm trying to create a shortcut on a remote desktop in the domain here, and I'm a domain admin. If I run the following code directly on the target machine, the shortcut can be created and is able to lead me to the target path.
$shortcutpath3 = "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "\\machine\testcustom\"
$Shortcut3.Save()
I saved this script as test.ps1, run it with folloing code on a different mahchine. The code ends without any errors/warings, and the shortcut is created on the target machine with the propeties i specified. But it cannot lead me to the target place, it actually ask me to pick a program to open that file. I compared the properties of the 2 shortcuts, and found that the "target type" of the broken shortcut is "file" while it is "file folder" for a good shortcut.
Invoke-Command -ComputerName TARGETSERVER -FilePath test.ps1
Any idea how i can fix this? And why is this happening? Thank!!!
I had the same problem and and I used Get-Item to make it work. Try this:
$targetPath = Get-Item("\\machine\testcustom\")
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = $targetPath.FullName
$Shortcut3.Save()
Since you're a domain admin I'd strongly recommend doing this with a Group Policy Preference. You can restrict shortcut creation to particular users/groups/computers/etc. via item-level targeting.
I've been battling this for the past several hours, Googling to no avail. For posterity, here's my summary. While alternatives suggestions are appreciated:
PowerShell inexplicably has no direct way to create a shortcut. It can create symbolic link, but 1) it requires admin rights, and 2) it behaves differently.
Group Policy Preferences are great--but only if your machines are in office or routinely on a VPN.
If you're trying to create a shortcut to a network folder, setting the TargetPath to that folder only works if the computer actually can reach it when the script runs, i.e. same issue as with Group Policy Preferences. PowerShell will create the shortcut, but the Target Type will be a File rather than a File Folder (and I found no info online how to control that; trailing slash or not doesn't matter).
The behavior of running these code suggestions varies depending whether you run this interactively or as a script.
Here's what I found does work:
Set the $shortcut.TargetPath to "C:\Windows\Explorer.exe"
Add a line: $shortcut.Arguments = """Target folder""" (the """ are needed as an escape character for the shortcut to show as C:\Windows\Explorer.exe "Target folder")
So for your example above, the following should work (assuming permissions to c:\Users\Public\Desktop):
$shortcutpath3 = "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "C:\Windows\Explorer.exe"
$shortcut.Arguments = """\\machine\testcustom\"""
$Shortcut3.Save()