r/PowerShell • • Apr 23 '21

How to Sign a PowerShell Script (And Run It)

Hey guys,

u/junecastillote just wrote a shiny new blog post you may enjoy.

"How to Sign a PowerShell Script (And Run It)"

Summary: Are you wondering what script signing is and how to distinguish a signed script from a non-signed one? Look no further! In this article, you'll learn how to sign PowerShell scripts using digital certificates.

https://adamtheautomator.com/how-to-sign-powershell-script/

130 Upvotes

19 comments sorted by

10

u/charliesk9unit Apr 24 '21

Noob here. Is the signed script only valid on the computer the certificate was generated?

13

u/[deleted] Apr 24 '21 edited Jun 11 '23

.

8

u/tehiota Apr 24 '21

Code signing provides identification of who claims to have written the script via their certificate signature. You still need to tell the computer whether or not you trust that signature. If you do, it will allow all scripts signed by that certificate to run.

In an enterprise environment you can trust the certificate via GPO and push it out to all computers in the domain.

2

u/charliesk9unit Apr 24 '21

Thanks, u/gorlechov and u/tehiota. I am in a AD environment so I'll have to look into how to push the certificate to the few machines the signed scripts will run on.

2

u/vellius Apr 24 '21

Create a "Code Signing" certificate

Sign scripts with private key

Install public key on servers in the trusted publisher repository.

PS Policy to "remotely signed"

8

u/[deleted] Apr 23 '21

Thanks for this. I planned to play with signing scripts in powershell this weekend in the lab.

3

u/HappyDadOfFourJesus Apr 24 '21

Caveat: Just because a script is signed DOESN'T mean it's safe to run in your environment.

3

u/gordonv Apr 24 '21

That's why you know who signed it.

2

u/projects67 Apr 24 '21

Can anyone here tell me why a non-signed script runs no problem as a startup script (via GPO) even though the computer PS execution policy is “restricted. ?” I assume by design, just curious why.

4

u/RockSlice Apr 24 '21

Considering how simple it is to bypass, PS execution policy should be considered a fat-finger safety measure, anyway, and not a security measure. Sort of like yellow safety tape.

3

u/dasookwat Apr 24 '21

It is intended to be this way: execution policy is a measure to prevent stupid actions, it's not a security measure at all.

2

u/[deleted] Apr 24 '21

Just start it with execution bypass ether way

1

u/vellius Apr 24 '21

I would guess the script runs under a "system" credential profile... probably has restrictions bypassed.

1

u/projects67 Apr 24 '21

Perhaps. But I believe I have sign in scripts running under the users profile too- and those also run too.

1

u/vellius Apr 24 '21

Then i suspect your policies are being applied in the wrong scope.

1

u/projects67 Apr 24 '21

I’m a novice. What do you mean? I have a slew of startup (machine) and logon (user) scripts that run via GPO

1

u/vellius Apr 25 '21

When you apply a policy on a machine (ex: RemoteSigned).. you need to specify a scope or else... it's applied only to your current console or user.

Using Localmachine would make sure that the policy is applied to everything

1

u/khaffner91 Apr 24 '21

It's a shame we can't sign powershell scripts with pwsh on Linux or Mac.

1

u/odaat2004 Apr 24 '21

Thanks for this. Never occurred to me to just ask my employer for an Authenticode certificate til I read this. That they probably actually have one available for users if you just ask. Now, to find the right service now request for it.