Get-Help
Whenever you come across a command, called cmdlet, in PowerShell you don't understand, you can always use the Get-Help cmdlet.
![]() |
| Get-Help Example (Click for larger image) |
Write-Host
Write-Host does exactly that, it writes customized output to the host, aka the console windows. It can't be used, to the best of my knowledge, for anything else. For example, it wont pipe into an Out-File cmdlet, but more on Out-File and piping later, for now, let's write our first bit of code:
Write-Host "Hello World"
![]() |
| Hello World! |
.\HelloWorld.ps1
![]() |
| HelloWorld.ps1 |
ForEach
ForEach is a loop construct that allows you to move through an object, such as an array, assigning each element to a variable to be worked with. Let's make our little script from before slightly more complicated.
$HelloWorld = "H","e","l","l","o"," ","W","o","r","l","d","!"
ForEach ($letter in $HelloWorld)
{
Write-Host $letter -nonewline
}
Write-Host
Let us examine each line here. The first line establishes an array called $HelloWorld. There are a couple of different ways to establish an array, but I will use this one for now. It's the next line we want to focus on, ForEach. This is the beginning of the loop construct and, in my opinion, one of the easiest to master. What happens here is: The script loops through the $HelloWorld array and assigns each element to the variable $letter. Then using our friend Write-Host with the switch -nonewline, which prevents carriage returns between the letters, we display our message. The last Write-Host after the loop gives us a new line after the message is written.
A quick note about how PowerShell works. You can take the above code, and save it as a .ps1 file, nothing wrong with that, but PowerShell has the ability to do a line by line execution straight from the console window. Now, to me, that's amazing. Just look at the above code in this example:
![]() |
| Line by Line Execution (Click for a larger image) |
In conclusion, I haven't even begun to scratch the surface of what is possible with PowerShell. The is pretty much no way anything I have put down today could be of practical use, but this is just the start. Once I get this up to speed with what I know, I'll begin to add other elements as I learn them
Coming Next
Next week, I will try to cover: Read-Host, String Manipulation, Aliases, and Piping. All very useful, and a great next step for the beginner. Until then...




String objects have a ToCharArray() method. Thus this gives you a ticker like output.
ReplyDeleteforeach ($character in ("Hello world").toCharArray()) {
write-host $character -nonewline
sleep -Milliseconds 100
}
But you can also use the ForEach object cmdlet which has an alias of ForEach. This is more PowerShell-ish.
("Hello world").toCharArray() | foreach {
write-host $_ -nonewline
sleep -Milliseconds 100
}
Wow. Thanks, Jeff! That's the first I've ever seen the ToCharArray() method. I really appreciate the tip! I'm always open to learning as much as I can
ReplyDeletePipe objects to Get-Member to discover what you can do with them. In many instances there are cmdlets that you should use instead of invoking a method directly. But for things like Strings and DateTime objects there's a lot of fun to be had.
ReplyDeleteGood luck.