Created
August 29, 2024 15:41
-
-
Save figueroadavid/a58221c9ca833768e089f4102ddc5e94 to your computer and use it in GitHub Desktop.
pInvoke function to retrieve 8.3 shortnames in powershell
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
| function Get-ShortPathName{ | |
| Param([string] $path) | |
| $MethodDefinition = @' | |
| [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)] | |
| public static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int cbShortName); | |
| '@ | |
| $Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru | |
| $shortPath = New-Object System.Text.StringBuilder(500) | |
| $retVal = $Kernel32::GetShortPathName($path, $shortPath, $shortPath.Capacity) | |
| return $shortPath.ToString() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment