2017-08-14 30 views
8

如何啓動使用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" /> 

這似乎與.LNK對「這臺電腦」,「資源管理器」的問題,等等

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" 

我試過這種方法,以及還沒有牽制MYCOMPUTER開始

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 
+0

快捷方式我讀了一次,通過GUID訪問這些特殊文件夾:請參閱https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx –

+0

*「這將固定到啓動exe的罰款「* ...這正是'我的電腦'(資源管理器)和Internet Explorer(iexplore.exe)是什麼。你想做什麼__exactly__不起作用? – gravity

+0

@gravity如果給我SomePath \ ThisPC.lnk。我如何找到這個路徑的目標來賦予上面的函數? – nlstack01

回答

6

大多數特殊文件夾都可以使用特殊值訪問,這裏記錄:ShellSpecialFolderConstants enumeration

所以,如果你想獲得我的電腦(又名。 「這臺電腦」)的文件夾,你可以這樣做:

$shell = new-object -com Shell.Application 
$folder = $shell.NameSpace(17) # "ssfDRIVES" constant 

,這將讓你一個Folder對象。

還有另一種方式使用文件夾CLSID(guid)。它將允許您訪問所謂的shell命名空間中的任何文件夾,甚至可能未在上面的枚舉中定義的文件夾(例如第三方shell命名空間擴展名)。語法是這樣的:

$shell = new-object -com Shell.Application 
$folder = $shell.Namespace("shell:::{CLSID}") 

事實上,這個有趣的語法結合'shell:' URI monikershell namespace parsing syntax :: {} CLSID。

例如要得到我的電腦文件夾,你會做使用稱爲CLSID_MyComputer這樣的常數:

$shell = new-object -com Shell.Application 
$folder = $shell.Namespace("shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}") 

這也將工作:

$shell = new-object -com Shell.Application 
$folder = $shell.Namespace("shell:MyComputerFolder") # direct shell: syntax 

它應該帶給你返回與上一次調用相同的對象。它們都是等價的。

一旦你有一個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() 

好,那就是爲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 

事實是,這時候,我們做「附到'開始」我的電腦正是Windows那樣,它會在C:\Users\<my user>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

+0

感謝您的迴應。我更新了這篇文章。找到動詞現在工作正常,但釘住不會發生使用.DoIt()。沒有任何反應 – nlstack01

+0

@ nlstack01 - 的確如此。我已經更新了我的回答 –

+0

不幸的是仍然無法將它交給Pin。它創建快捷方式很好,但不會發生固定。我嘗試在C:\ Users \ \ AppData \ Roaming \ Microsoft \ Windows \ Start Menu \ Programs中創建快捷方式 – nlstack01