Created
June 8, 2021 14:25
-
-
Save ChadDevOps/bf7dd290ae92f383e88ecc911028a37a to your computer and use it in GitHub Desktop.
Create random short name for Windows Directories
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
#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