Valrok
Valrok

Reputation: 1574

PowerShell concatenation

I'm trying to write a simple line of code that will delete various user files from a C disk for various servers. How do I concatenate with PowerShell to get the pathway to a server?

For instance, this is what I'm trying to do, but PowerShell isn't recognizing the + symbol as concatenation I think:

remove-item "\\$server" + '\C$\Documents and Settings\a409126' -force -recurse -whatif

I get an error saying:

Remove-Item : A positional parameter cannot be found that accepts argument '+'.

Upvotes: 2

Views: 5756

Answers (3)

Michał Pawlak
Michał Pawlak

Reputation: 106

You want remove items from path which you should join, so:

Join-Path "\\$server" "C$\Documents and Settings\a409126" |Remove-Item -Force -Recurse -Whatif

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126762

One way is to use variable expansion:

Remove-Item "\\$server\C$\Documents and Settings\a409126" -Force ...

Another way is with the Join-Path cmdlet:

Join-Path -Path \\$server -ChildPath 'C$\Documents and Settings\a409126'

Upvotes: 2

Akim
Akim

Reputation: 8679

Try this:

remove-item ("\\$server" + '\C$\Documents and Settings\a409126') -force -recurse -whatif

You need to know more about command mode vs. expression mode to be effective in PoSH

Upvotes: 6

Related Questions