Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Created August 29, 2024 15:41
Show Gist options
  • Select an option

  • Save figueroadavid/a58221c9ca833768e089f4102ddc5e94 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/a58221c9ca833768e089f4102ddc5e94 to your computer and use it in GitHub Desktop.
pInvoke function to retrieve 8.3 shortnames in powershell
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