Adrian Danlos
Adrian Danlos

Reputation: 447

How to include variable in a path?

I am trying to create a file called creation_DDMMYYYY where DDMMYYYY is the current date. I'm getting an incorrect path format error on the path tho.

$nowDate=$(date)
New-Item -ItemType file "$HOME/creation_$nowDate.txt" 

As I have just started with Powershell I really don't know what's the issue here...

Upvotes: 1

Views: 729

Answers (1)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

Because you use just $date in your nowDate the path is incorrect because is includes spaces (folder name can't be with spaces) and is totally not the format you want.

Just change the nowDate to get the correct date format and it will work:

$nowDate = Get-Date -Format ddMMyyyy
New-Item -ItemType file "$HOME/creation_$nowDate.txt"
# Result:
Folder Properties...        Name
                            ----
                            creation_02062019.txt

Upvotes: 1

Related Questions