PowerShell tutorial 7

There are many free scripts on the net you can use with powershell, but the real power comes in making your own cmdlets. To begin, first start by enabling scripts.

type this at the prompt; set-executionpolicy RemoteSigned

Now all you need to do is to write up your scripts in notepad or a similar editor. If you are used to writing batch files then this will be familiar. Remember to save your new script with a .ps1 extention ie. (filename.ps1)

Once you’ve finished writing your script, save it somewhere on your drive that is easy to find, such as c:psscripts . Now that you’ve done that you’ll find it’s often easier to remember if you give your script an alias like so;

new alias alias-name path-to-script.ps1 or
new alias pingsweep c:psscriptspingsweep.ps1

now when you type pingsweep in powershell it will go fetch your script from c:psscripts and specifically execute the pingsweep.ps1 script.

PowerShell also has the ability to perform robust loops.
While loops for setting criteria at the beginning of a loop.
Do while loops for evaluating the criteria statement at the end of a cycle.
Foreach loops to pull in items from a group of values (in PowerShell parlance, this is known as a collection).
For loops or For statements to perform an action on a subset of values

take a look at this script which pings all addresses on a network and returns a 0 as a result if the address is available.

1..254| ForEach -Process {WmiObject -Class Win32_PingStatus -Filter (“Address=’192.168.16.” + $_ + “‘”) -ComputerName .} | Select-Object -Property Address, StatusCode | ft

here you see the looping abilities of powershell pretty well.

I’ll show you more example scripts in later posts, but for homework take a look at the many free scripts on the web and see if you can identify the ones that could be helpful to you and learn from them.

Leave a Reply