Reputation: 361
So, I'm tryng to create a folder, where i merge 2 strings and Get Date; like this:
2345__OPEXs_datetime
My code:
Get-ChildItem 'C:\Scripts\MetadataExport' | New-Item -Name (Get-ChildItem 'C:\Scripts\MetadataExport' -Name).Split('-')[2] + '_XIPs' + "_$((Get-Date).ToString('yyyy-MM-dd'))" -ItemType Directory -Force
But it's gives-me the follow error:
New-Item : A positional parameter cannot be found that accepts argument '_XIPs'.
At line:1 char:45
+ ... taExport' | New-Item -Name (Get-ChildItem 'C:\Scripts\MetadataExport' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand
Can you help-me to figure it out what i'm doing wrong?
Best
Upvotes: 0
Views: 44
Reputation: 440556
If you want to dynamically calculate argument values based on each input object from the pipeline, you need to use a delay-bind script block ({ ... }
), inside of which the automatic $_
variable refers to the input object at hand:
Get-ChildItem C:\Scripts\MetadataExport | New-Item -Type Directory -Force -Name {
$_.Name.Split('-')[2] + '_XIPs' + "_$((Get-Date).ToString('yyyy-MM-dd'))"
} -WhatIf
Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
once you're sure the operation will do what you want.
Upvotes: 3