在很多情况下,我需要可执行文件或命令行工具的路径,例如:
notepad
或kubectl
。使用 PowerShell 时,可执行文件可用,但文件的物理位置并不总是很容易找到。
一种方法是搜索 PATH 上的每个文件夹,甚至更糟糕的是系统(
gci -r | % { $_ ...}
),但这不是最有效地利用时间,每次都重新编码。有更好的办法吗?
Get-Command
将返回一个包含多个带有路径名的字段的对象。例如,如果我在系统上输入 Get-Command notepad
,我会得到
PS Z:\> Get-Command notepad
CommandType Name Version Source
----------- ---- ------- ------
Application notepad.exe 10.0.18... C:\WINDOWS\system32\notepad.exe
如果我输入
Get-Command notepad | Select-Object *
我会得到
PS Z:\> Get-Command notepad | Select-Object *
HelpUri :
FileVersionInfo : File: C:\WINDOWS\system32\notepad.exe
InternalName: Notepad
OriginalFilename: NOTEPAD.EXE.MUI
FileVersion: 10.0.18362.1 (WinBuild.160101.0800)
FileDescription: Notepad
Product: Microsoft® Windows® Operating System
ProductVersion: 10.0.18362.1
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
Path : C:\WINDOWS\system32\notepad.exe
Extension : .exe
Definition : C:\WINDOWS\system32\notepad.exe
Source : C:\WINDOWS\system32\notepad.exe
Version : 10.0.18362.1316
Visibility : Public
OutputType : {System.String}
Name : notepad.exe
CommandType : Application
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters :
ParameterSets :
我最近发现
Where.exe
可用于 PowerShell。
> Where.exe kubectl
C:\Program Files\Docker\Docker\resources\bin\kubectl.exe
这与 PowerShell 变量兼容,因此可以在编写脚本时使用:
$path = where.exe notepad
“Get-Command”或“where.exe”限制太多,它们不能用于所有程序...... 以下 2 个函数可以正常工作,一个用于获取快捷方式信息,一个用于获取我在所有脚本中使用的应用程序信息:
Function Get-ShortCut {
param (
[string]$StringToSearch,
[array]$Directories = @(
"${Env:PROGRAMDATA}\Microsoft\Windows\Start Menu\Programs"
"${Env:APPDATA}\Microsoft\Windows\Start Menu\Programs"
),
[string]$TypeTargetFile = "exe"
)
$ShortCutArray = @()
$shellObject = New-Object -ComObject WScript.Shell
foreach ($Directory in $Directories) {
$shortcuts = Get-ChildItem -Path $Directory -Recurse -Filter *.lnk
foreach ($shortcut in $shortcuts) {
$ShortcutObject = $shellObject.CreateShortcut($shortcut.FullName)
if (($ShortcutObject.TargetPath -like "*$StringToSearch*.$TypeTargetFile") -or ($shortcut.BaseName -like "*$StringToSearch*")) {
$ShortCutArray += [PSCustomObject]@{
Name = $shortcut.BaseName
Path = $shortcut.FullName
TargetPath = $ShortcutObject.TargetPath
Arguments = $ShortcutObject.Arguments
WorkingDirectory = $ShortcutObject.WorkingDirectory
IconLocation = $ShortcutObject.IconLocation
Description = $ShortcutObject.Description
WindowStyle = $ShortcutObject.WindowStyle
Hotkey = $ShortcutObject.Hotkey
}
}
}
}
Return $ShortCutArray
}
Function Get-Application {
param (
[string]$StringToSearch
)
$hives = @(
"HKLM"
"HKCU"
)
$RegistryKeys = @(
"\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
"\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall"
)
$ApplicationArray = @()
ForEach ($hive in $hives) {
ForEach ($RegistryKey in $RegistryKeys) {
if (Test-Path "${hive}:${RegistryKey}" -ErrorAction SilentlyContinue) {
$Applications = Get-ChildItem "${hive}:${RegistryKey}" | ForEach-Object { Get-ItemProperty $_.PsPath } | Select DisplayName,DisplayVersion,InstallDate,UninstallString | Where-Object {$_.DisplayName -match "$StringToSearch"}
ForEach ($Application in $Applications) {
$DisplayName = $Application.DisplayName
$DisplayVersion = $Application.DisplayVersion
$UninstallString = $Application.UninstallString -Replace "`"",""
$InstallDate = $Application.InstallDate
$ApplicationShortCuts = Get-ShortCut $StringToSearch
if ($ApplicationShortCuts.Count -gt 1) {
$WorkingDirectory = $ApplicationShortCuts.WorkingDirectory[0]
$TargetPath = $ApplicationShortCuts.TargetPath[0]
$Arguments = $ApplicationShortCuts.Arguments[0]
} else {
$WorkingDirectory = $ApplicationShortCuts.WorkingDirectory
$TargetPath = $ApplicationShortCuts.TargetPath
$Arguments = $ApplicationShortCuts.Arguments
}
if ((-not $WorkingDirectory) -and $TargetPath) {$WorkingDirectory = Split-Path -Path $TargetPath -Parent}
$ApplicationArray += [PSCustomObject]@{
Name = $DisplayName
Version = $DisplayVersion
InstallDate = $InstallDate
Directory = $WorkingDirectory
Command = $TargetPath
Arguments = $Arguments
UninstallCommand = $UninstallString
}
}
}
}
}
if (-not $ApplicationArray) {
$ApplicationShortCuts = Get-ShortCut $StringToSearch
forEach($ApplicationShortCut in $ApplicationShortCuts) {
$ApplicationName = $ApplicationShortCut.Name
$WorkingDirectory = $ApplicationShortCut.WorkingDirectory
$TargetPath = $ApplicationShortCut.TargetPath
$Arguments = $ApplicationShortCut.Arguments
if ((-not $WorkingDirectory) -and $TargetPath) {$WorkingDirectory = Split-Path -Path $TargetPath -Parent}
$ApplicationArray += [PSCustomObject]@{
Name = $ApplicationName
Version = $Null
InstallDate = $Null
Directory = $WorkingDirectory
Command = $TargetPath
Arguments = $Arguments
UninstallCommand = $Null
}
}
}
Return $ApplicationArray
示例:
Get-Application Edge
Name : MSEdgeRedirect
版本:0.7.4.0 安装日期:20230830 目录:C:\Program Files (x86)\Microsoft\Edge\Application 命令:C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe 论据: 卸载命令:C:\Program Files\MSEdgeRedirect\MSEdgeRedirect.exe /uninstall
名称:Microsoft Edge 版本:114.0.1823.43 安装日期:20230612 目录:C:\Program Files (x86)\Microsoft\Edge\Application 命令:C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe 论据: UninstallCommand : C:\Program Files (x86)\Microsoft\Edge\ApplicationL.0.1823.43\Installer\setup.exe --uninstall --msedge --channel=stable --系统级别 --verbose-logging
名称:Microsoft Edge 更新 版本:1.3.175.29 安装日期: 目录:C:\Program Files (x86)\Microsoft\Edge\Application 命令:C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe 论据: 卸载命令: