Running and making scripts with PowerShell

Here is another small blog entry on how to use PowerShell. When you download and install this tool you want to get to work right away. However, there are a few small obstacles to sort out. There are a few reasons why Microsoft is focusing on security but I will not move further in on that discussion here. Let's just say that they do and that PS is no exception. The default setting for this scripting environment is ironically enough that you cannot run your own scripts on it:) In order to do this you have to change this you need to set the ExecutionPolicy. It is set to "Restricted" by default.


This policy can have four different values. "Restricted" will not load configuration tools or run any scripts. "AllSigned" will only run scripts and configuration files that have been signed by a trusted publisher. This also includes your own scripts. "RemoteSigned" is the one you would want to use if you only want to run your own scripts. It will still require that all scripts you download from the web have been signed. "Unrestricted" will lift every restriction except the ever-so-useful question: "Are you sure you want to do this??" when you run external scripts.

Still, as much as I am tired to get that question, I do appreciate a bit of security. Anyways, here is how to how to set PS to allow your own scripts to run.

PS C:\> Set-ExecutionPolicy remotesigned

You can now create you own script using you favourite editor. Mine for the moment is Notepad3.(KISS etc etc...)

PS C:\> np3 Test.ps1

We tell the script to create a simple variable and then echo it in the shell.

$foo = "Hello Nerd"
echo $foo

We run it by writing the script name and hitting enter. Note: It is important to understand how PS is running scripts. It will search through the Windows path in search for the script. This is done before searching the current directory. So if you have a directory in the path with a script wearing the same name, then that is the script that will run. So be careful:)

PS C:\> .\Test.ps1
Hello Nerd
PS C:\> _

Comments

Popular posts from this blog

Designing and programming - Part 2

Filtering Dropdown choices in a Power Pages form using Dataverse Relations

Exploring the Power of Variables in Liquid and Power Pages