1

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!

mklement0
  • 382,024
  • 64
  • 607
  • 775

1 Answers1

3

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

(New-Object -ComObject Scripting.FileSystemObject).GetFile($path).ShortPath
Start-Automating
  • 8,067
  • 2
  • 28
  • 47
  • I just had a follow up question on this. Since I am using a COM object here and I am very new to PowerShell, will it get disposed off gracefully? Or is there something that I have to explicitly code so that there are no memory issues or any other kind of COM related issues? Thanks – hillcountry99 Sep 08 '22 at 22:51
  • I did hit this page. But no mention of how to dispose it. https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-.net-and-com-objects--new-object-?view=powershell-7.2 – hillcountry99 Sep 08 '22 at 23:08