Reputation: 1574
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
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
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
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