Zhenkai
Zhenkai

Reputation: 328

How to execute Powershell script on the fly

If I have a Powershell script accessible through HTTP, how can I download it and execute it on the fly like what we can do in the bash?

curl --user user:pass https://fake.url | bash

Upvotes: 1

Views: 1173

Answers (2)

comphilip
comphilip

Reputation: 550

Try

curl --user user:pass https://fake.url | powershell -Command -

If the value of Command is "-", the command text is read from standard input.

Upvotes: 1

Joey
Joey

Reputation: 354566

First of all, you shouldn't do that. I mean, downloading random code from the web and running it without taking a look at it? Yikes!

Yes, it's convenient, but it's also a horribly stupid idea, just as curl | bash, or even worse, curl | sudo bash is.

In most cases you can probably use WebClient.DownloadString and pass it to Invoke-Expression: Invoke-Expression (New-Object Net.WebClient).DownloadString(«url»). I'm not sure whether that still opens a script: scope for variables, though, so if that scope is used in the script it might not work. In that case you can download it as a file and execute the script. Then you also have to take care of the execution policy, though.

Upvotes: 2

Related Questions