$code = { # place your code here (must be less than 4096 characters) # (this example generates a battery report on notebooks # and opens it in your default browser) $r = "$env:temp\report.html" powercfg /batteryreport /duration 14 /output $r Invoke-Item-Path$r Start-Sleep-Seconds2 Remove-Item-Path$r }
# turn code into a one-liner, remove comments, escape double-quotes # NOTE: this is a very simplistic conversion. Does not support block comments # or quoted double quotes or any edgy stuff # USE with simple staight-forward code only $oneliner = $code.ToString().Trim().Replace('"','\"'). Replace([Char[]]10,'').Split([Char[]]13). Trim().Where{!$_.StartsWith('#')} -join';'
# create path to a link file. It is always placed on your desktop # and named "clickme.lnk" $desktop = [Environment]::GetFolderPath('Desktop') $linkpath = Join-Path-Path$desktop-ChildPath'ClickMe.lnk'
# create a shortcut file $com = New-Object-ComObject WScript.Shell $shortcut = $com.CreateShortcut($linkpath) # minimize window so PowerShell won't pop up $shortcut.WindowStyle = 7 # use a different icon. Adjust icon index if you want $shortcut.IconLocation = 'shell32.dll,8' # run PowerShell $shortcut.TargetPath = "powershell.exe" # submit code as an argument $shortcut.Arguments = "-noprofile $oneliner"
# save and create the shortcut file $shortcut.Save()
# this is the long path to convert $path = "C:\Program Files\PowerShell\7\pwsh.exe"
# signature of internal API call $signature = '[DllImport("kernel32.dll", SetLastError=true)] public static extern int GetShortPathName(String pathName, StringBuilder shortName, int cbShortName);' # turn signature into .NET type $type = Add-Type-MemberDefinition$signature-Namespace Tools -Name Path -UsingNamespace System.Text
# create empty string builder with 300-character capacity $sb = [System.Text.StringBuilder]::new(300) # ask Windows to convert long path to short path with a max of 300 characters $rv = [Tools.Path]::GetShortPathName($path, $sb, 300)
# output result if ($rv-ne0) { $shortPath = $sb.ToString() } else { $shortPath = $null Write-Warning"Shoot. Could not convert $path" }
但是,如何获得默认长路径名称的短路径名称呢?一种方法是使用 Windows 脚本宿主使用的旧 COM 组件:
1 2 3 4 5 6 7 8 9 10 11
# take any path # in this example, I am taking the path where Powershell 7 is installed # this requires that PowerShell 7 in fact is installed. # You can use any other path as well $path = (Get-Command-Name pwsh).Source
"Long path: $path"
# convert it to 8.3 short name $shortPath = (New-Object-ComObject Scripting.FileSystemObject).GetFile($path).ShortPath "Short path: $shortPath"
结果看起来像这样:
Long path: C:\Program Files\PowerShell\7\pwsh.exe
Short path: C:\PROGRA~1\POWERS~1\7\pwsh.exe
# this fails when help defaults to show ONLINE help PS> help about_for Get-Help : The online version of this Help topic cannot be displayed because the Internet address (URI) of the Help topic is not specified in the command code or in the help file for the command.
# the -ShowWindow parameter always shows local help in an extra window PS C:\> help about_for -ShowWindow
PS> Update-Help-Verbose VERBOSE: Resolving URI: "https://go.microsoft.com/fwlink/?LinkId=717973" VERBOSE: Your connection has been redirected to the following URI: "https://pshelpprod.blob.core.windows.net/cabinets/powershell-5.1/" VERBOSE: Performing the operation "Update-Help" on target "Microsoft.PowerShell.LocalAccounts, Current Version: 5.2.0.0, Available Version: 5.2.0.0, UICulture: en-US". VERBOSE: Microsoft.PowerShell.LocalAccounts: Updated C:\Users\USERNAME\Dokumente\PowerShell\Help\Microsoft.PowerShell.LocalAccounts\1.0.0.0\en-US\Microsoft.Powershell.LocalAccounts.dll-Help.xml. Culture en-US Version 5.2.0.0 VERBOSE: Resolving URI: "https://go.microsoft.com/fwlink/?linkid=2113632" VERBOSE: Your connection has been redirected to the following URI: "https://pshelp.blob.core.windows.net/powershell/help/7.0/Microsoft.PowerShell.Management/" VERBOSE: Performing the operation "Update-Help" on target "Microsoft.PowerShell.Management, Current Version: 7.0.1.0, Available Version: 7.0.1.0, UICulture: en-US". VERBOSE: Microsoft.PowerShell.Management: Updated C:\Users\USERNAME\Dokumente\PowerShell\Help\en-US\Microsoft.PowerShell.Commands.Management.dll-Help.xml. Culture en-US Version 7.0.1.0 ...
要使用下载的本地帮助文件,您可以在想了解的命令后添加 “-?“ 通用参数:
1 2 3 4 5 6 7 8
PS> Get-Process -?
NAME Get-Process
SYNOPSIS Gets the processes that are running on the local computer or a remote computer. ...
# calculate the parent paths that can contain profile scripts $Paths = @{ AllUser_WPS = $pshome CurrentUser_WPS = Join-Path-Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath"WindowsPowerShell" AllUser_PS = "$env:programfiles\PowerShell\*" CurrentUser_PS = Join-Path-Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath"PowerShell" }
# check all paths for PowerShell scripts ending on "profile.ps1" $Paths.Keys | ForEach-Object { $key = $_ $path = Join-Path-Path$paths[$key] -ChildPath'*profile.ps1' Get-ChildItem-Path$Path | ForEach-Object { # create a custom object with all relevant details for any # found profile script
# name of PowerShell host is the prefix of profile file name if ($_.Name -like'*_*') { $hostname = $_.Name.Substring(0, $_.Name.Length-12) } else { $hostname = 'any' } [PSCustomObject]@{ # scope and PowerShell version is found in the # name of the parent folder Scope = $key.Split('_')[0] PowerShell = $key.Split('_')[1]