0

Through the Command Prompt, I am looking to open Powershell as Admin and pass a command for it to execute. The command is simple: remove all .txt files in a directory. I am having a difficult time passing this command into the Admin Powershell. Here's what I'm trying:

powershell -Command "Start-Process powershell -Verb runAs | Remove-Item -Path C:\MyFolder\*.txt"

So far, the Powershell window opens but nothing is executed.

2 Answers 2

1

Problem

You are just piping the $null output of Start-Process to Remove-Item, which makes no sense.

Solution

Your 2nd Powershell invocation should use -Command parameter:

powershell -command Start-Process powershell -Verb runAs -ArgumentList @('-Command', 'Remove-Item', \"'C:\MyFolder\*.txt'\")

The command-line that follows -command doesn't need to be quoted, which simplifies nested quoting.

The somewhat strange looking quoting for the Remove-Item argument is required to support path with spaces. First the backslash-escaped double quotes will be resolved by the cmd parser, finally the Remove-Item call will look like this:

Remove-Item 'C:\MyFolder\*.txt'
Sign up to request clarification or add additional context in comments.

Comments

0
powershell -Command  "start-process powershell -Verb RunAs -Args 'Remove-Item -Path C:\MyFolder\*.txt'"

you can also pass remove-item as args in string format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.