Skip to content

Instantly share code, notes, and snippets.

@ChadDevOps
Created June 8, 2021 14:25
Show Gist options
  • Save ChadDevOps/bf7dd290ae92f383e88ecc911028a37a to your computer and use it in GitHub Desktop.
Save ChadDevOps/bf7dd290ae92f383e88ecc911028a37a to your computer and use it in GitHub Desktop.
Create random short name for Windows Directories
#Generates a random shortname for directories
#Enable Shortnames in Windows 10
# fsutil 8dot3name query
# fsutil behavior set disable8dot3 0
#Run as admin
Get-ChildItem -Recurse -Directory | ForEach-Object {
$fullpath = $_.FullName
$foldername = $_.Name
$ShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($_.FullName).ShortPath
$ShortName = $ShortPath|split-path -leaf
if($ShortName.Contains('~')) {
echo "'$foldername' is MSDOS: $ShortName"
}else{
$first = $foldername.SubString(0,1)
$num = Get-Random -Minimum 1000 -Maximum 9999
$newShortName = $first + $num + '~1'
echo "creating MSDOS $newShortName for '$foldername'"
cmd.exe /c fsutil file setshortname "$ShortPath" $newShortName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment