0

Someone can tell me if is possible pass variables when i call Remove-Printer command on Powershell?

Remove-Printer -Name 'Printer1 (Copy $i)'

Idk if this is possible but i tried this:

for ($i=0; $i -le 5; $i ++) {
   Remove-Printer -Name 'Printer1 (Copy '+$i+')'
}

and more unsuccessful attempts...

If someone can help me... thanks you.

1
  • I don't known much about PowerShell, but I believe that you don't need the + to concatenate strings; probably 'Printer1 (Copy $i)' will work. Check this: For-Loop Commented Apr 12, 2019 at 17:36

1 Answer 1

1

The single quotes tells PowerShell that there are character literals inside. Just use double quotes, and $i had a space after it (the $i++ part) in the for loop constructor.

for ($i=0; $i -le 5; $i++) {
   Remove-Printer -Name "Printer1 (Copy $i)"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help! I forgot to comment that I need to execute this from cmd and does not accept " when i execute #PowerShell -Command "...."
Solved! PowerShell -Command ""for ($i=0; $i -le 5; $i++){Remove-Printer -Name """Printer1 (Copy $i)"""}""
You should be able to lose the beginning and trailing quotes. (before for and after the closing bracket)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.