简体   繁体   English

如何使用Powershell在桌面上创建磁盘快捷方式?

[英]How to create Disk shortcut on desktop using Powershell?

I'm trying to create a shortcut to my disk C on desktop using PowerShell. 我正在尝试使用PowerShell在桌面上创建磁盘C的快捷方式。

$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. 问题在于它创建了快捷方式,但是在C驱动器上存在一些随机位置。
My Desktop is on E drive. 我的桌面在E盘上。 How is it possbile that the shortcut is created on C drive if the path is E:\\Users\\Oskar. 如果路径为E:\\ Users \\ Oskar,则如何在C驱动器上创建快捷方式。

Does anyone know how to solve this issue?. 有谁知道如何解决这个问题?
Thanks in advance, Oskar. 预先感谢,奥斯卡。

$env:VAR looks for an environment variable named VAR . $env:VAR查找名为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. 因此,您的"$env:E:\\Users\\Oskar.lnk"查找一个名为E:\\Users\\Oskar.lnk的环境变量,该变量不存在,因此使用空字符串。

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). 这会使您的快捷方式保存到(Win32进程的)当前目录中(注意:这不一定与PowerShell的当前目录相同)。

You should use $ShortcutFile = "E:\\Users\\Oskar.lnk" . 您应该使用$ShortcutFile = "E:\\Users\\Oskar.lnk"

Similarly for the other place where you've used $env: ... 同样,对于您使用$env:的其他地方$env: ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Powershell 创建以管理员身份运行的快捷方式 - How to create a Run As Administrator shortcut using Powershell 如何在用户桌面上创建计算机的快捷方式 - How to create shortcut to Computer on the user's desktop PowerShell:在用户之间的桌面快捷方式中静态使用 $env:userprofile - PowerShell: Using $env:userprofile in a desktop shortcut statically between users 如何使用Visual Studio安装程序在所有应用程序菜单中创建桌面应用程序的快捷方式 - How to create shortcut to desktop app in the all apps menu using Visual Studio Installer 以编程方式为桌面“快捷方式”创建快捷键组合 - Programmatically create shortcut keys combo for desktop “shortcut” 如何创建启动 Cygwin 并跟踪特定文件的桌面快捷方式? - How to create a desktop shortcut that launches Cygwin and tails a particular file? 如何使用vbscript删除多个文件夹,桌面和开始菜单快捷方式 - how to delete multiple folders,desktop and start menu shortcut using vbscript 如何将 Powershell 中的 Windows 快捷方式创建到文件名中包含特殊字符的文件? - How to create a Windows Shortcut in Powershell to a file with special characters in filename? 使用 PowerShell 创建启动 PowerShell 脚本的快捷方式 - Use PowerShell to create a shortcut to launch a PowerShell script 无法在桌面上为远程IP文件创建快捷方式 - Unable to create shortcut on Desktop for a remote IP file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM