Home > Software design >  Get-Content splits path with spaces in powershell
Get-Content splits path with spaces in powershell

Time:01-15

I want to get the content of a simple text file with powershell.

This is my code:

$versionPath = "$PSScriptRoot/../VERSION"
Get-Content $versionPath -Raw

I get this error:

C:\Users\Rik : The term 'C:\Users\Rik' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
  C:\Users\Rik van\source\repos\
  FullyQualifiedErrorId : CommandNotFoundException

Get-Content splits the path by spaces, I tried the suggestions here but nothing seems to work. any suggestions?

CodePudding user response:

My spidey-sense says you're calling your script on the command line like this:

C:\> powershell C:\Users\Rik van\source\repos\myscript.ps1

The problem isn't the content of your script - it's the path to it.

PowerShell is treating the space as the end of the path to the script, and assumes the rest of the filename is parameters to pass to it. The only problem is it can't find a PowerShell script called C:\Users\Rik.

If you wrap the filename in quotes it'll probably just work:

C:\> powershell "C:\Users\Rik van\source\repos\myscript.ps1"
  •  Tags:  
  • Related