Reputation: 45
I'd like to create a folder based on a parameter used in the name, so for example:
$Website = "test"
new-item \\172.1.1.1\C`$\Folder\$Website_Archive -type directory
The _Archive needs to be part of the folder name but I cannot figure out how to pass the $Website with the _Archive included.
Here is the error I get:
New-Item : Item with specified name \172.1.1.1\C$\Folder\ already exists. At line:2 char:9 + new-item <<<< \172.1.1.1\C`$\Folder\$Website_Archive -type directory + CategoryInfo : ResourceExists: (\172.1.1.1\C\Folder\:String) [New-Item], IOException + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
If I remove the '_Archive' from the line it will create the folder named "test". I'm assuming it's looking for a parameter of $Website_Archive and not finding it so it tries to just recreate the root folder. How can I get the parameter passed into the folder name?
Thanks!
BB
Upvotes: 1
Views: 16126
Reputation: 301587
Apart from what @JohnL mentions, I prefer string formatting, especially when many variables are involved:
"\\172.1.1.1\C`$\Folder\{0}_Archive" -f $Website
Upvotes: 4
Reputation: 4002
Either escape as per JNK's answer, enclose $Website with $(...)
new-item "\\17.1.1.1\C`$\Folder\$($Website)_Archive" -type directory
Upvotes: 3
Reputation: 65217
Just escape the underscore with a backtick
new-item "\\17.1.1.1\C`$\Folder\$Website`_Archive" -type directory
Upvotes: 4