Home > Software engineering >  How to get folder names with ~ in PowerShell
How to get folder names with ~ in PowerShell

Time:09-09

I have this code in PowerShell

$workingDir = $PSScriptRoot   "/"

This will give me the path to the current ps1 file. I am later on referencing this working Directory variable to refer to sub folders and to run some old 1990's style programs.

My working directory contains a folder name with spaces. "Shared Files".

How can I get the old DOS convention name of: "shared~1" as the folder name (with a tilde) for the above folder using PowerShell functions, without hard coding names or manual replacement of folder names?

I need this because some of those old programs don't recognize folder names with spaces.

Thanks!

CodePudding user response:

You should be able to use the COM object Scripting.FileSystemObject to get the short path:

(New-Object -ComObject Scripting.FileSystemObject).GetFile($path).ShortPath
  • Related