Last week I showed some of the most basic PowerShell cmdlets. And I was shown something that takes me nicely into this week. In the example I was given, there was use of string manipulation, pipelines, and aliases.
Pipelines
PowerShell is designed to allow one cmdlet to be piped into another, passing the results of one cmdlet to the next one. So in the example, the ForEach-Object cmdlet received an array of characters from the result of the string method invoked. Then within the loop construct, each element is returned individually using the $_ variable which refers to the current object being processed. The exact inner workings of this is something I'm still understanding
String Manipulation
The example shows another topic I wanted to cover, string manipulation. Using a string method, a string was converted into a character array, same outcome of what I did without as much typing. Less typing is less room for error. You could easily write a complicated script that finds every letter O in a string and replace it with a 0, or you could just use the .Replace() method. Here's an example
(“Hello world”).ToCharArray() | ForEach
{
if ($_ -eq “o”)
{
$_ = “0”
}
Write-Host $_ -nonewline
}
This will result in the exact same result as with this example:
(“Hello world”).Replace('o','0')
As Jeff pointed out, you can pipe a string into the Get-Member cmdlet. This will return all the properties on any given object. So we can assign a value to a string and pipeline it to the Get-Member cmdlet. For example:
$foo = “Hello world”
$foo | Get-Member
You get this:
![]() |
| Get-Member (Click to enlarge) |
Anything with a MemberType of Method can be used to manipulate the value of the string. Some Methods require parameters in order to execute properly, other do not. For example:
Write- Host $foo.ToUpper()
Will display the value of the object $foo in all capital letters. However, if you try this:
Write-Host $foo.Substring()
You will get an error
![]() |
| Method Error (Click to enlarge) |
Looking at the Get-Member again for the object $foo, listed under Definition you can see what parameters are required for each Method. Some end in just (), not requiring a parameter. However, .Substring() is listed as Substring(int startIndex), string Substring(int startIndex, int length). This shows that at the minimum, the .Substring() method requires at least one number, and at most two. So, for example:
Write-Host $foo.Substring(6,5)
Returning just the second half of the phrase. This has all sorts of useful implications. First, it wouldn't do anyone good to have a script fail because the user entered “YES”, and it was only checking for the value of “yes”, or even just “y”. By using different Methods you can get it down to just what you want and still maintain versatility for the user.
Methods exist for all types of objects, but strings have to be the most common use for methods.
When it comes to strings, chances are you will typical used, hard coded string values as above. While there are reasons for doing so, you can simply do like Jeff did and work with a constant string like (“Hello World”). Still an object, but only one that PowerShell keeps around long enough to use it than forgets all about it. The true power of strings comes in the for of user interaction. Which brings us to Read-Host.
Read-Host
Read-Host allows the script to take direct input for the user. This allows the user to input the value of an object each time the script is executed. Scripting normally all about automation, and you typically get your input from elsewhere, but in some cases, it's best to let the user decide what the value is. In this example, I'll get the value of an object when the script is run and process it within the script.
$firstName = Read-Host “What's you first Name”
$lastName = Read-Host “What's you last name”
$fullName = $firstName + “ “ + $lastName
Write-Host “Your full name is $fullName”
There is just something satisfying about making a computer greet the world then say your name. The most common use for user interface is to get the name of an object you want to use in an up coming cmdlet. I'll show examples of that when I cover other cmdlets.
Alias
An Alias is a like a nickname for a cmdlet. The example from last week showed an example of an alias, with the ForEach alias for the ForEach-Object cmdlet. Another alias for ForEach-Object is %. For a list of useable aliases you can use, just type Get-Alias into the console.
![]() |
| Get-Alias (Click to enlarge) |
So, to change directories, sl will do the same thing as Set-Location.
![]() |
| Set-Location (Click to enlarge) |
Next Week
Next week I'm going to start the with the scripting challanges from the 2010 Scripting Games and complete with the first one and do one a week, writing scripts in PowerShell to do so. I'll post my scripts here with the best write up I can to help understand what each script is doing. That gives me something to do over then next ten weeks, and I hear it's a great way to learn. Here's the link: 2010 Scripting Games - All Links on One Page. The first challenge is to search the local registry and check for an entry, and either add it or update it. New stuff for me. Until then...
Next week I'm going to start the with the scripting challanges from the 2010 Scripting Games and complete with the first one and do one a week, writing scripts in PowerShell to do so. I'll post my scripts here with the best write up I can to help understand what each script is doing. That gives me something to do over then next ten weeks, and I hear it's a great way to learn. Here's the link: 2010 Scripting Games - All Links on One Page. The first challenge is to search the local registry and check for an entry, and either add it or update it. New stuff for me. Until then...




No comments:
Post a Comment