Shortcut not showing TargetPath [duplicate] - powershell

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

Related

Powershell: Path as input parameter is cropped [duplicate]

This question already has an answer here:
How to access a PSDrive from System.IO.File calls?
(1 answer)
Closed 1 year ago.
I have a powershell-script that I want to execute through the cammand-line. For this I need to input paths to xml files. Its better to show the problem.
Error: File not Found
I have all my files in the same directory C:\Users\fynne\test (reason for that are the users I'm writing this script for) but somehow the test directory is skipped while loading the xml (C:\Users\fynne\01-38-029.xml not found). I have no idea why this happens.
This is how I load the xml.
$PatientA = $args[0]
$PatientB = $args[1]
$XmlA = New-Object System.XML.XMLDocument
$XmlA.Load($PatientA)
$XmlB = New-Object System.XML.XMLDocument
$XmlB.Load($PatientB)
Does somebody knows why and has a fix for it? I know that I can fix it by using some string manipulation and $pwd however I rather prefer not to.
Thx
PowerShell will attempt to resolve non-absolute paths relative to the current provider location - but .NET methods (like XmlDocument.Load()) will resolve them relative to the working directory of the current process instead.
You can manually convert a relative path to an absolute one with Convert-Path:
$PatientA,$PatientB = $args[0..1] |Convert-Path
$XmlA = New-Object System.XML.XMLDocument
$XmlA.Load($PatientA)
$XmlB = New-Object System.XML.XMLDocument
$XmlB.Load($PatientB)
If you want to attempt further validation of the path, you can also use Resolve-Path which includes provider metadata:
$PatientA = $args[0] |Resolve-Path
if($PatientA.Provider.Name -ne 'FileSystem'){
throw 'First path was not a file system path...'
return
}

Bypass Set up Internet Explorer 11 pop up window using powershell

I try to bypass Windows IE setting pop up from the initial launch IE. either close the window or click ask later. Is it possible Powershell can check the window object? I tried the "New-Object" below:
New-Object -ComObject 'internetExplorer.Application'
But it doesn't seem to work that way I expected.
Thanks
For those looking for the Registry method (or Powershell), this is the script I use:
$keyPath = 'Registry::HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Internet Explorer\Main'
if (!(Test-Path $keyPath)) {
New-Item $keyPath -Force
}
Set-ItemProperty `
-Path $keyPath `
-Name "DisableFirstRunCustomize" `
-Value 1
You want to test the path exists first as using the -Force parameter on New-Item will remove any existing children of the path provided.
Is using group policy out of the question as this can easily be done through a gpo. Here is a link explaining. https://mkcheah88.blogspot.com/2014/06/microsoft-how-to-disable-internet.html
If not is it ok to change the registry key this links to with powershell prior to internet explorer opening? If so I can modify this answer to include that answer.

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()