I'm trying to create a shortcut to my disk C on desktop using PowerShell.
$TargetFile = "$env:C:\"
$ShortcutFile = "$env:E:\Users\Oskar.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()
The problem is it creates a shortcut but in some random localization on C drive.
My Desktop is on E drive. How is it possbile that the shortcut is created on C drive if the path is E:\\Users\\Oskar.
Does anyone know how to solve this issue?.
Thanks in advance, Oskar.
$env:VAR
looks for an environment variable named VAR
.
So your "$env:E:\\Users\\Oskar.lnk"
looks for an environment variable named E:\\Users\\Oskar.lnk
, which doesn't exist, so it uses an empty string.
This causes your shortcut to be saved to the (Win32 process's) current directory (note: this is not necessarily the same as PowerShell's current directory).
You should use $ShortcutFile = "E:\\Users\\Oskar.lnk"
.
Similarly for the other place where you've used $env:
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.