Last active
February 17, 2026 05:54
-
-
Save sba923/54a260dbd0fcd6672de13cf9e81b41db to your computer and use it in GitHub Desktop.
Resolve path to short (8.3) version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Resolves the 8.3 short path (DOS-style) of a given filesystem path. | |
| .DESCRIPTION | |
| This script uses the Windows FileSystemObject COM interface to retrieve the short (8.3 format) | |
| path of a given file or folder. It supports both files and directories, recursively resolving | |
| all path components to their short names. | |
| The output is a custom PowerShell object that includes the full resolved path, along with an | |
| added `ShortPath` property, which shows the equivalent 8.3 format path. | |
| Only filesystem paths are supported. | |
| .PARAMETER Path | |
| The full path to a file or folder whose 8.3 short path should be resolved. | |
| .EXAMPLE | |
| .\Resolve-ShortPath.ps1 -Path 'C:\Users\MyName\Documents\Long Folder Name' | |
| Returns an object containing the original resolved path and its corresponding 8.3 short path. | |
| .OUTPUTS | |
| Custom PSObject with standard path properties and an additional `ShortPath` property. | |
| #> | |
| #requires -version 7.4 | |
| # this is one of Stéphane BARIZIEN's public domain scripts | |
| # the most recent version can be found at: | |
| # https://gist.github.com/sba923/54a260dbd0fcd6672de13cf9e81b41db#file-resolve-shortpath-ps1 | |
| param([Parameter(Mandatory = $true)][string] $Path) | |
| # derived from https://devblogs.microsoft.com/scripting/use-powershell-to-display-short-file-and-folder-names/ | |
| $item = Get-Item -Path $Path -ErrorAction SilentlyContinue | |
| if ($null -eq $item) | |
| { | |
| Write-Error ("Cannot find path '{0}' because it does not exist." -f $Path) | |
| $null | |
| Exit(1) | |
| } | |
| if ($item.PSProvider.Name -ne 'FileSystem') | |
| { | |
| Write-Error ("Only filesystem paths are supported") | |
| $null | |
| Exit(1) | |
| } | |
| $fso = New-Object -ComObject Scripting.FileSystemObject | |
| function GetShortPath([string] $Path) | |
| { | |
| $item = Get-Item -LiteralPath $Path -Force | |
| if ($item.PSIsContainer) | |
| { | |
| if ($item.PSParentPath -eq '') | |
| { | |
| return $item.FullName | |
| } | |
| $shortname = $fso.getfolder($item.FullName).ShortName | |
| $parentpath = Split-Path -Path $Path -Parent | |
| return (Join-Path -Path (GetShortPath -Path $parentpath) -ChildPath $shortname) | |
| } | |
| else | |
| { | |
| $shortname = $fso.getfile($item.FullName).ShortName | |
| $parentpath = Split-Path -Path $Path -Parent | |
| return (Join-Path -Path (GetShortPath -Path $parentpath) -ChildPath $shortname) | |
| } | |
| } | |
| # get a PathInfo object for the item, then clone it so we're able to set the DefaultDisplayPropertySet | |
| $resolvedpath = (Resolve-Path -Path $Path) | Select-Object -Property * | |
| # compute the full short path i.e. short name for each of the path components | |
| $shortpath = GetShortPath -Path $Path | |
| # add ShortPath property | |
| Add-Member -InputObject $resolvedpath -MemberType NoteProperty -Name ShortPath -Value $shortpath | |
| # set the default property to 'ShortPath' | |
| # derived from https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/ | |
| #Give this object a unique typename | |
| $resolvedpath.PSObject.Typenames.Insert(0, 'ResolvedShortPath') | |
| #Configure a default display set | |
| $defaultDisplaySet = 'ShortPath' | |
| #Create the default property display set | |
| $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [string[]]$defaultDisplaySet) | |
| $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) | |
| $resolvedpath | Add-Member MemberSet PSStandardMembers $PSStandardMembers | |
| # output the final object | |
| $resolvedpath | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment