Ricardo Polo Jaramillo
Ricardo Polo Jaramillo

Reputation: 12318

New-Item error in powershell

I found this interesting behavior. Why could this happen

I am using the cmdlet New-Item for create a folder. If I write something like this it works

New-Item "D:\logeando9091" -type directory 

I have a variable of type string. It contains the value of the path where I want the new folder.

Why when Use New-Item with this variable it fails?

New-Item $settings.get_Item("LogFolder") -type directory

Here is a screenshot enter image description here

Upvotes: 2

Views: 2770

Answers (1)

Joey
Joey

Reputation: 354446

Your string contains double quotes around it, for whichever reason, so of course this doesn't work. Remove them and you should be good:

$logFolder = $settings.get_Item("LogFolder") -replace '^"|"$'
mkdir $logFlder

You should probably fix the code that reads the setting, though.

Another way would be using Invoke-Expression but I wouldn't recommend it. Better to avoid it.

Upvotes: 3

Related Questions