Playing with String in PowerShell

As being a Windows Administrator I use PowerShell every day and the best part of it is, I learn something new every day. In simple word PowerShell can make your life easier, and I think you all agreed with it. I often requested to pull various reports from various sources and it was not always possible to get the exact report by executing one or more cmdlet. Sometimes had to manipulate certain strings from cmdlet output or reading some specific log files for generating reports. Here I will share my experience on how we can easily working with strings in PowerShell.

TASK – 1: Validate the target computer is located in Non-Prod OU or Not.

First I kept the Computer Object LDAP path to $InputComputerName variable using

Now I have to split the variable with “,”, so that I will be getting it into the following order.

Now you can each line by using [0], [1], [2], and exactly here I need the value of second line, so I used [1]

Now again I am splitting this with “=”

No I am selecting second line with [1] to get the current OU location and mission accomplished J

TASK – 2: Extract the IP v4 from ipconfig output.

First I kept the output of ipconfig to a variable $SysIP

Now I am searching the string IPv4 from the variable.

Now I am splitting the line with “:”

Now I am selecting second line by using [1], but still some fix is required as there is space in front of IP Address

To get rid of it, I used Trim

You can also do it in a single line code using this, cool J

TASK -3: Get the Citrix PV Ethernet driver version from AWS Console log.

First I kept the console log to the variable $GetConsole

Now I used findstr to search the string “Ethernet”

Once I got the required line I split it with “:”

Now I selected the last line as I am getting the required information there.

And finally I am removing the extra space in front of that line. J

Alternatively we can also do it like this way,

First I search for “Ethernet” and split the output

Now I am selecting my required value from line 3 to line 7

And finally I am joining the line to get the value what I need, funny right?

TASK -4: Get the backup success failure report from log file with the date.

First I kept the required line from my application backup log file to the variable $LastBackups

Now I am splitting it.

After splitting it, I have to remove blank lines using Trim

Since I need the backup date, I selected the 3rd line using [2]

And the status available on 9th line so I selected it using [9]

Again I have to split it to get the status only

Trim it to remove blank lines

Select the second line to get the status using [1]

Have to split it again to get the exact status

Finally selecting the status from first line with [0]. Cool J

TASK -5: Print my name in upper case and reverse order

First keeping my name in a variable $Name

Changing my name string to upper case and in array

Reversing my name using [array]::reverse()

Finally joining the array to string. Now my name is in reverse order with upper case. ATAGUAS

Please do let me know if you like this article.

Leave a Reply

Your email address will not be published. Required fields are marked *