nls*_*k01 8 c# windows powershell
如何使用PowerShell启动特殊文件夹?像"ThisPC",iexplorer
这将启动exe的罚款,但是Windows资源管理器和myComputer呢?如何锁定这些物品,因为它们没有目标?
鉴于这种
<start:DesktopApplicationTile Size="2x2" Column="0" Row="0" DesktopApplicationLinkPath="%APPDATA%\Microsoft\Windows\Start Menu\Programs\Windows System\This PC.lnk" />
Run Code Online (Sandbox Code Playgroud)
它似乎与.lnk的"这台PC","文件资源管理器"等有问题
Function PinLnk
{
Param
(
[Parameter(Mandatory,Position=0)]
[Alias('p')]
[String[]]$Path
)
$Shell = New-Object -ComObject Shell.Application
$Desktop = $Shell.NameSpace(0X0)
$WshShell = New-Object -comObject WScript.Shell
$Flag=0
Foreach($itemPath in $Path)
{
$itemName = Split-Path -Path $itemPath -Leaf
#pin application to windows Start menu
$ItemLnk = $Desktop.ParseName($itemPath)
$ItemVerbs = $ItemLnk.Verbs()
Foreach($ItemVerb in $ItemVerbs)
{
If($ItemVerb.Name.Replace("&","") -match "Pin to Start")
{
$ItemVerb.DoIt()
$Flag=1
}
}
}
}
PinLnk "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
Run Code Online (Sandbox Code Playgroud)
我尝试了这种方法,但仍然没有固定计算机来启动
PS C:\WINDOWS\system32> Function PinLnk14
>> {
>>
>> $shell = new-object -com Shell.Application
>> $folder = $shell.NameSpace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") # ssfDRIVES
>> $ItemVerbs=$folder.Self.Verbs()
>> Foreach($ItemVerb in $ItemVerbs)
>> {
>> Write-Output $ItemVerb.Name
>> If($ItemVerb.Name.Replace("&","") -match "Pin to Start")
>> {
>> Write-Output "TRYING TO PIN"
>> $ItemVerb.DoIt()
>> }
>> }
>> }
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Pinlnk14
&Open
Pin to Quick access
Mana&ge
&Pin to Start
TRYING TO PIN
Map &network drive...
Dis&connect network drive...
Create &shortcut
&Delete
Rena&me
P&roperties
Run Code Online (Sandbox Code Playgroud)
可以使用特殊值访问大多数特殊文件夹,如下所示:ShellSpecialFolderConstants枚举
因此,如果您想获取"我的电脑"(又名"此PC")文件夹,您可以执行以下操作:
$shell = new-object -com Shell.Application
$folder = $shell.NameSpace(17) # "ssfDRIVES" constant
Run Code Online (Sandbox Code Playgroud)
这将获得一个Folder对象.
还有另一种使用文件夹CLSID(guid)的方法.它将允许您访问所谓的shell命名空间中的任何文件夹,甚至是可能未在上面的枚举中定义的文件夹(例如,第三方shell命名空间扩展).语法是这样的:
$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:::{CLSID}")
Run Code Online (Sandbox Code Playgroud)
实际上,这个有趣的语法将'shell:'URI名字对象与shell名称空间解析语法 :: {CLSID} 结合在一起.
因此,例如,要获取"我的电脑"文件夹,您将使用如下所示的常量CLSID_MyComputer:
$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
Run Code Online (Sandbox Code Playgroud)
这也有效:
$shell = new-object -com Shell.Application
$folder = $shell.Namespace("shell:MyComputerFolder") # direct shell: syntax
Run Code Online (Sandbox Code Playgroud)
它应该带回你与前一次调用相同的对象.他们都是等同的.
一旦你有了一个Folder对象,就有了获取相关动词的最后一个技巧,因为Verbs()方法只在FolderItem对象上定义.要从文件夹中获取FolderItem(作为文件夹也是命名空间中的"项目",因此它还有一个FolderItem外观),您可以使用Self属性,如下所示:
$shell = new-object -com Shell.Application
$folder = $shell.NameSpace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") # ssfDRIVES
$folder.Self.Verbs()
Run Code Online (Sandbox Code Playgroud)
好的,那就是获取FolderItem的动词.然而,要固定项目以启动,"Pin to Start"动词调用并不总是有效(例如,对于"我的电脑"),或者动词甚至不可用(例如,对于标准文件夹).一般来说,由于某种原因,它对文件夹的效果并不好.
因此,文件夹的一个解决方案是首先在该文件夹(包括我的电脑或其他特殊位置)的某处创建一个快捷方式文件(.lnk),然后将该快捷方式文件固定到该文件夹中.注意:Pin to Start的标准(非语言本地化)动词是" PinToStartScreen ",最好使用它来扫描各种动词(所有动词都有规范名称).所以代码看起来像这样固定我的电脑开始:
$wshell = new-object -com WScript.Shell
$shortcut = $wshell.CreateShortcut("c:\temp\mypc.lnk")
$shortcut.TargetPath = "shell:MyComputerFolder" # use the same syntax as described above
$shortcut.Save()
$shell = new-object -com Shell.Application
$folder = $shell.NameSpace("c:\temp")
$lnk = $folder.ParseName("mypc.lnk") # get the shortcut file
$lnk.InvokeVerb("PinToStartScreen") # invoke "Pin To Start" on the shortcut file
Run Code Online (Sandbox Code Playgroud)
事实上,当我们在"我的电脑"上执行"Pin to Start"时,它正是Windows所做的,它会创建一个快捷方式 C:\Users\<my user>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |