Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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"
$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