I was able to find how to use the GetInvalidFileNameChars() method in a PowerShell script. However, it seems to also filter out whitespace (which is what I DON
GetInvalidFileNameChars()
I wanted spaces to replace all the illegal characters so space is replaced with space
$Filename = $ADUser.SamAccountName [IO.Path]::GetinvalidFileNameChars() | ForEach-Object {$Filename = $Filename.Replace($_," ")} $Filename = "folder\" + $Filename.trim() + ".txt"