How to strip illegal characters before trying to save filenames?

前端 未结 6 830
夕颜
夕颜 2020-12-08 20:04

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

6条回答
  •  有刺的猬
    2020-12-08 20:30

    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"
    

提交回复
热议问题