user3431504
user3431504

Reputation: 159

Powershell not reading spaces correctly

Simple issue, I'm trying to run the following command:

$d = ((Get-Date).AddMonths(-1)).ToString('MMM')
$filepath = "<<PATH>>\EOM " + $d + " " + $d.Year
New-Item $filepath -type directory

Path works correctly, that is a parameter set by an external program.

I can't get it to recognise the " " + $d.Year in the path, it just stops after $d. Is there an easy way I can get it to read the whole file path? At the moment It keeps creating a new folder ...\EOM Sep without the year.

Upvotes: 0

Views: 42

Answers (1)

StephenP
StephenP

Reputation: 4081

There is no Year property of $d as you explicitly cast it to a string with ToString('MMM') Also instead of trying to concatenate spaces use a string format:

$d = ((Get-Date).AddMonths(-1))
$filepath = "<<PATH>>\EOM {0} {1}" -f $d.ToString('MMM'),$d.Year
New-Item $filepath -type Directory

Upvotes: 1

Related Questions