How To Load A Custom Function In PowerShell

OR – How To Set Security Permissions To Run Other People’s PowerShell Scripts

I had to configure a custom PowerShell function on my clustered file server to try to fix a permissions issue I ran into after one of my SANs failed. Google was nice enough to point me here http://learn-powershell.net/2014/06/24/changing-ownership-of-file-or-folder-using-powershell/ where I found a solution to my problem. When you’re in crisis mode, it can sometimes take you a minute to remember to set security correctly to run a downloaded .ps1. So, since I rarely forget to do something I blog, this is more for me than for you 😉

On the bottom of Boe’s blog, there is a download link that sends you to the TechNet Gallery to download the .ps1 file.  For the sake of simplicity I downloaded it to C:\Users\mrichardson\Set-Owner.ps1 since that is the root folder PowerShell opens in. If this is a function (or script) you anticipate using frequently, a better location would probably be your Modules folder.  Browse to the function/script you downloaded, right click on the file, go to Properties, then on the General Tab, and click the “Unblock” button at the bottom.

Unblock POSH

Next, open PowerShell with Administrator Privileges and set the Execution Policy to “Remote Signed”.  This will allow you to run a script or function from a different system.  You set the Execution Policy by running the following PowerShell command:

Set-ExecutionPolicy remotesigned

Now you can load the function by running the following command (substituting the appropriate file name).

. ./Set-Owner.ps1

You should now be able to use the function as per its article. Happy scripting!

Advertisements