OK… Powershell effectively has two ways of being used. You can type straight onto the command line or you can script. This is similar to your traditional command prompt (cmd) where you could open cmd and type your command you want or you can open notepad and write your command into a .bat script file. I have given a few commands in previous posts but I haven’t explained how to make an actual script yet, or even what a script is.
If you have been working with computers for a while (especially if your in an admin/tech person role) you will have come across .bat, .vbs, .kix or possibly Perl or Java script files. Good new for everyone is that we can say bye bye to them and say hello to the .ps1 file.
Something to be aware of before trying to run a .ps1 file in powershell is that you will need to sign your script or make sure that powershell has it’s execution policy set to remote signed. To check what your current execution policy is set to, in powershell console type: get-executionpolicy . To view available options type get-help set-executionpolicy and that will give you more details on this. If you don’t want to open up your script execution policy then your can sign your scripts, I won’t go into that on this blog as I haven’t done this yet, when I do give it a go (sometime soon) I will blog how to do it (or how I did it – hopefully that should be like how you are meant to do it?).
Anyway I have waffled around the subject of this post for long enough so I will get straight onto it.
You can make a .ps1 file in many ways, the most basic way being opening notepad, writing some Powershell commands, and saving with the extension .ps1 at the end of the file name. If you would prefer to have Powershell on tap whilst writing your script then try the Windows Powershell ISE. This will give you a tabbed scripting window, a Powershell command input console and a shared output window. This is a useful environment to script in because it allows you to write and test either sections or the whole of your script from the same window you are working in. The Powershell command input console also allows you to run get-member and help on anything you are putting into your script.
Now we know what the script is when should we write one and for what?
When to write a script seems to be a common question, generally if you are planning to use a command regularly then why not make a script and save wearing out your keyboard? In my last post I gave the script for a making a powershell logo, it wasn’t very useful but it’s a script and a good example for this post. Many tutorials you find online will be working with making a script for a task, by starting with the basics you can be ready to work on anything you find online. You can use a script for many a task, think what you do regularly and think, “Can I script this?”
One last point that I won’t really go into on this post (big topic so needs it’s own but it ties in with this) is your profile. Your profile is something that loads when you open up powershell, you can set lots of useful stuff in there and it’s editable by you. I currently have a very basic profile which I have set to change my default directory, load my powershell logo script (it calls in a ps1 file to do that) then clears my screen ready for use. It’s not much but it’s all I need so far. As I head off onto a future post I will steer back to the current one but pointing out that your profile is basically a ps1 file that loads when powershell loads.
At this point I think we now a bit about scripts and I’ll hopefully be guiding you through more of what to do with them in up coming posts so check back again and thanks for reading.
Dave.