Omglolyes
Omglolyes

Reputation: 184

Using command as parameter in PowerShell

I am trying to move some files to a relative subfolder recursivly.

I want to use the get-location command and combine it with a string ("\foo_1"), while using it as a parameter.

So get-location should result in "D:\foo_2" so that the result is "D:\foo_2\foo_1"

All while being a parameter of a separate command:

Get-ChildItem -Path ".\*.wav" -Recurse | Move-Item -Destination (get-location + "string")

I know i could just use variables, but i really want to understand this (if there is an answer). Thanks in advance =)

Upvotes: 1

Views: 54

Answers (1)

boxdog
boxdog

Reputation: 8432

You could use a sub-expression, like this:

Get-ChildItem -Path ".\*.wav" -Recurse | 
    Move-Item -Destination "$(Get-Location)\foo_1"

The structure $(...) tells PowerShell to execute the contents of the brackets first (i.e. run Get-Location) then substitute the result into the string. Note that you must use double-quotes as this functionality doesn't work with single-quoted strings.

Upvotes: 2

Related Questions