【问题标题】:How to pin to start menu using PowerShell如何使用 PowerShell 固定到开始菜单
【发布时间】:2012-04-11 19:53:48
【问题描述】:

我可以使用 PowerShell 将一些程序固定到 Win7 上的任务栏。

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace('C:\Windows')    
$item = $folder.Parsename('notepad.exe')
$verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Tas&kbar'}
if ($verb) {$verb.DoIt()}

如何修改上述代码以将程序固定到开始菜单?

【问题讨论】:

  • 不要。这是用户的决定,不是你的。实际上,与任务栏相同。没有对此故意的编程访问。
  • 为什么?有偏见吗?此外,我尝试将它用于开始菜单。但它不起作用。
  • 我必须这样做。因为,我想创建一个标准的开发者机器。所有机器必须相同。任务栏、开始菜单……当开发者换机时,以后一定不能有协和问题
  • 在您推出的 Windows 映像中有一些方法可以做到这一点。 IIRC 通过系统映像管理器和无人值守安装的应答文件。

标签: powershell windows-7 startmenu


【解决方案1】:

使用下面的代码

$shell = new-object -com "Shell.Application"  
$folder = $shell.Namespace('C:\Windows')    
$item = $folder.Parsename('notepad.exe')
$verb = $item.Verbs() | ? {$_.Name -eq 'Pin to Start Men&u'}
if ($verb) {$verb.DoIt()}

注意:更改在第四行。

【讨论】:

  • 这将适用于当前用户,我怎样才能为所有用户固定它?
【解决方案2】:

在此处查看脚本(国际):http://gallery.technet.microsoft.com/scriptcenter/b66434f1-4b3f-4a94-8dc3-e406eb30b750

如果您想在 Modern UI 界面 (Windows 8) 中添加 Pin 之类的操作,请在 $verbs 处添加 51201

【讨论】:

  • 不仅仅是提供链接,it would be preferable 在此处包含答案的基本部分,并提供链接以供其他参考。如果您不能胜任这项任务,您应该考虑在问题上简单地leaving a comment 而不是发布答案。
【解决方案3】:

另一种方式

$sa = new-object -c shell.application
$pn = $sa.namespace($env:windir).parsename('notepad.exe')
$pn.invokeverb('startpin')

或取消固定

$pn.invokeverb('startunpin')

【讨论】:

  • 这是迄今为止我见过的最好的答案。
  • 如何设置要显示的名称?
【解决方案4】:

大多数解决方案的主要问题是它们枚举文件上的动词,搜索执行操作的字符串(“Pin to Startmenu”等),然后执行它。如果您需要在公司中支持 30 多种语言,这将不起作用,除非您使用外部函数来搜索本地化命令(请参阅来自 shtako-verflow 的答案)。

Steven Penny 的答案是第一个语言中立且不需要任何外部代码的答案。它使用存储在注册表中的动词HKEY_CLASSES_ROOT\CLSID\{90AA3A4E-1CBA-4233-B8BB-535773D48449}HKEY_CLASSES_ROOT\CLSID\{a2a9545d-a0c2-42b4-9708-a0b2badd77c8}

基于此,这是我们现在使用的代码:

function PinToTaskbar {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
  ExecuteVerb $FilePath "taskbarpin" 
}

function UnpinFromTaskbar {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
  ExecuteVerb $FilePath "taskbarunpin" 
}

function PinToStartmenu {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
 ExecuteVerb $FilePath "startpin" 
}

function UnpinFromStartmenu {
         param([Parameter(Mandatory=$true)][string]$FilePath)  
 ExecuteVerb $FilePath "startunpin" 
}

function ExecuteVerb {  
         param(
            [Parameter(Mandatory=$true)][string]$File,
            [Parameter(Mandatory=$true)][string]$Verb
         ) 

    $path = [System.Environment]::ExpandEnvironmentVariables($File) 
    $basePath = split-path $path -parent   #retrieve only the path File=C:\Windows\notepad.exe -> C:\Windows
    $targetFile = split-path $path -leaf    #retrieve only the file File=C:\Windows\notepad.exe -> notepad.exe

    $shell = new-object -com "Shell.Application"  
    $folder = $shell.Namespace($basePath)        
    if ($folder)
    {
       $item = $folder.Parsename($targetFile) 

       if ($item)
       {
         $item.invokeverb($Verb)
         # "This method does not return a value." (http://msdn.microsoft.com/en-us/library/windows/desktop/bb787816%28v=vs.85%29.aspx)
         # Therefore we have no chance to know if this was successful...
         write-host "Method [$Verb] executed for [$path]"       
       } 
       else
       {
         write-host "Target file [$targetFile] not found, aborting" 
       }             
    }
    else 
    {      
      write-host "Folder [$basePath] not found, aborting" 
    }

}


#PinToTaskbar "%WINDIR%\notepad.exe"
#UnpinFromTaskbar "%WINDIR%\notepad.exe"

PinToStartmenu "%WINDIR%\notepad.exe"
#UnpinFromStartmenu "%WINDIR%\notepad.exe"

【讨论】:

    【解决方案5】:

    Steven Penny 上面的第二个答案对我来说效果很好。这里还有一些花絮。

    它通过 PowerShell 执行 COM,因此您几乎可以使用任何 COM 客户端执行相同的操作。例如,这是一个 AutoHotkey 版本。

    Shell := ComObjCreate("Shell.Application")
    Target := Shell.Namespace(EnvGet("WinDir")).ParseName("Notepad.exe")
    Target.InvokeVerb("startpin")
    

    VBScript 或 InnoSetup 看起来几乎相同,除了用于创建对象的函数。

    我还发现我有一个固定好的程序,但由于编译器的限制,没有正确的图标和/或描述。我刚刚制作了一个小小的 1 行 WinForms 应用程序,它使用 Process.Start 启动目标,然后在 AppInfo.cs 的 Title 属性的开始菜单中添加了适当的图标和我想要的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2021-11-14
      相关资源
      最近更新 更多