Brady
Brady

Reputation: 395

Multiple Variables for "-name" parameter in "New-Item"

I'm having issues with the following PS script:

New-Item -name $InfoLog -path $LogPath -Name ("Info Log - ",$DateStamp," - ",$TimeStamp) -type file

It gives me the error-

Cannot bind parameter because parameter 'Name' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3".

Any ideas? I also tried it without the parentheses.

Upvotes: 1

Views: 7446

Answers (1)

user2555451
user2555451

Reputation:

All PowerShell cmdlets accept only one argument per parameter. However, you passed two arguments to the -Name parameter of New-Item:

New-Item -name $InfoLog -path $LogPath -Name ("Info Log - ",$DateStamp," - ",$TimeStamp) -type file
# One argument ^^^^^^^^     Another argument ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Because this is an illegal function call, PowerShell is raising your error.


It looks like you meant to write this:

New-Item -Path $LogPath -Name "Info Log - $DateStamp - $TimeStamp" -Type File

The variables in the string "Info Log - $DateStamp - $TimeStamp" will be expanded into the values that they represent:

PS > $a = 123   
PS > $b = "abc"
PS > "$a -- $b"
123 -- abc

PS > 

Upvotes: 5

Related Questions