0

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?

Raul Saucedo
  • 1,614
  • 1
  • 4
  • 13
nathan
  • 3
  • 1
  • 3
    Does your simple textfile not have an Extension? Your `$versionPath` to me looks like the path to a folder.. Also, did you type in the dots to obscure the path for this question specifically or is that the real Full path you're using? – Theo Jan 12 '22 at 15:52
  • 1
    ...also, use the `-LiteralPath` parameter – iRon Jan 12 '22 at 15:58

1 Answers1

0

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"
mclayton
  • 8,025
  • 2
  • 21
  • 26