PS Simple Code

I just wanted to take some time and go over a couple basic powershell features I ran across. You can see that it does have its usefulness (mainly the ability to use pipes), especially compared to the older batch files.

Get-Process | Where-Object{$_.Name -like "s*"} | `
Sort-Object -Property WS -Descending | `
Select-Object -First 10 | Out-File Out.txt

This program searches all running processes on the computer, sends it through the pipe where the Where-Object command selects only the processes that begin with the letter s and sends those to the Sort-Object command which then sorts those processes (which are really objects and not just strings) by their WS property in a descending order. This sorted array is then send to the Select-Object command which picks only the first 10 elements and writes it to the file Out.txt

Another interesting part of Powershell is its ability to search/match using REGEX. Here is an example.

$data = Get-Content file.csv
$totalLen = $data.count

$nonMatchTotal = 0
forEach($entry in $data){
  if(!($entry -match "(1|[a-z]).*@[a-z].*\.[a-z]")){
      $nonMatchTotal += 1

Write-Host "Number of non matches: $nonMatchTotal"
This entry was posted in Programming, Scripting and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s