在许多情况下,我需要一个可执行文件或命令行工具的路径,例如:notepad或kubectl。虽然使用PowerShell,可执行文件是可用的,但文件的物理位置并不总是很容易找到。一种方法是搜索PATH上的每个文件夹,甚至更糟的是搜索系统(gci -r | % { $_ ...}),但这不是最有效地利用时间,每次都要重新编码。有更好的方法吗?
notepad
kubectl
gci -r | % { $_ ...}
niwlg2el1#
Get-Command将返回一个对象,该对象包含多个带有路径名的字段。
Get-Command
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 *,
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 :
型
1l5u6lss2#
我最近发现Where.exe可用于PowerShell。
Where.exe
> Where.exe kubectl C:\Program Files\Docker\Docker\resources\bin\kubectl.exe
字符串这与PowerShell变量兼容,因此可以在编写脚本时使用:$path = where.exe notepad
$path = where.exe notepad
du7egjpx3#
“Get-Command”或“where.exe”的限制太多,它们不能用于所有程序.
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 Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand:C:\Program Files\MSEdgeRedirect\MSEdgeRedirect. exe/uninstall姓名(N):微软Edge版本:安装日期:20230612目录:C:\Program Files(x86)\Microsoft\Edge\Application Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand:C:\Program Files(x86)\Microsoft\Edge\Application\Setup.exe--uninstall --msedge --channel=stable --system-level --verbose-logging名称:微软Edge更新版本:1.3.175.29安装日期:目录:C:\Program Files(x86)\Microsoft\Edge\Application Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand:
3条答案
按热度按时间niwlg2el1#
Get-Command
将返回一个对象,该对象包含多个带有路径名的字段。字符串
如果我输入
Get-Command notepad | Select-Object *
,型
1l5u6lss2#
我最近发现
Where.exe
可用于PowerShell。字符串
这与PowerShell变量兼容,因此可以在编写脚本时使用:
$path = where.exe notepad
du7egjpx3#
“Get-Command”或“where.exe”的限制太多,它们不能用于所有程序.
字符串
} }个文件夹
型
}
范例:
型
版本:0.7.4.0安装日期:20230830目录:C:\Program Files(x86)\Microsoft\Edge\Application Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand:C:\Program Files\MSEdgeRedirect\MSEdgeRedirect. exe/uninstall
姓名(N):微软Edge版本:安装日期:20230612目录:C:\Program Files(x86)\Microsoft\Edge\Application Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand:C:\Program Files(x86)\Microsoft\Edge\Application\Setup.exe--uninstall --msedge --channel=stable --system-level --verbose-logging
名称:微软Edge更新版本:1.3.175.29安装日期:目录:C:\Program Files(x86)\Microsoft\Edge\Application Command:C:\Program Files(x86)\Microsoft\Edge\Application\msedge.exe参数:UninstallCommand: