$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 blank string of 260 chars $blanker = " " * 260
# 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 and prepend with a blank string # so payload is hidden in the properties dialog $shortcut.Arguments = "$blanker-noprofile $oneliner"
# save and create the shortcut file $shortcut.Save()
$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