Scenario 1: Use static Format method
The System.String .NET Framework class has a Format static method that accepts
composite formatting techniques.
Some times you you have use string with variable values in some sort of operation like
combining string and create one path
Example
PS C:\> [string]$file = 'file.txt'
PS C:\> [string]::Format("c:\test{0}",$file)
c:\test\file.txt
Example
PS C:\> [string]$file = 'file.txt'
PS C:\> "c:\test\{0}" -f $file
c:\test\file.txt
Piping commands can save quite a lot of time. Another way of looking at piping is stringing items or commands together. In this tutorial, you will learn how to string PowerShell commands together.
If you have not already done so, click open Windows PowerShell ISE.
Usually, in MS-DOS or Windows Command prompt, piping will be ran much like the following code:
The command performs directory listing of c:\windows\system32 and then pipes the output to the find command to filter for lines that contain the string ".exe". When the same command is ran in PowerShell, we receive an error of course. Keep in mind that various changes have been made that commands such as the one above no longer work in PowerShell, it is implemented in another way.
Recall that DIR is an alias to Get-ChildItem, so by running the code below (one at a time), you will receive the appropriate output in the three formats provided to you per line.
Example
Get-ChildItem c:\windows\system32 | Format-Table
Get-ChildItem c:\windows\system32 | Format-List
Get-ChildItem c:\windows\system32 | Format-Wide
Below is a small glimpse of what each output would appear like by format listing, the listing will come out to be several pages long of course.
Table Format List Format Wide Format
As you see, Get-ChildItem continues to return the same exact data every time, regardless in which format it is presented in.
Step two.
Output looking too clean? Of course! Every command that is typed in the console is automatically piped to Out-Default. What is Out-Default? It is a Cmdlet that is in charge of figuring out how to render the output of a given command, if no format is specified of course.
A Few Last Words...
Every object has a view registered to it. The particular view defines which formatter to use. We discuss output more in depth in another tutorial. For additional Windows PowerShell Commands Please see following links..
There are many ways in which one can manipulate the objects. The simplest object to deal with and the one we are going to be working with in this tutorial is a string. The following code displays, Dan Daman:
Example
$firstname = "Dan"
$lastname = "Daman"
$fullname = $firstname + " " + $lastname
write-output $fullname
Let us manipulate the text so that all characters are displayed in uppercase. String objects have a method called ToUpper() that returns the object in all uppercase letters. The transformation is typed as follows:
Example
$firstname = "Dan"
$lastname = "Daman"
$fullname = $firstname + " " + $lastname
Write-output "Length of full name: " + $fullname.length
Write-output "Full name is: " + $fullname.ToUpper()
The output will be presented like the following:
Notice how parenthesis were not used for length but were definitely used for ToUpper. The reason behind this is because properties are directly available values, while ToUpper is an actual method that needs to be called. The dot between $fullname an ToUpper(), is used to tell PowerShell that whatever is after the period is the property of the method of whatever is before the period. Think of it as "owner.owned".
Objects are instances of classes. Classes can be thought of as definitions, schemas or types. They define what objects are to look like. Because an object is an instance of a class, an instance of a class Dog might be your neighbor's dog Fifi. In a virtual world, an object is the instance of a class that you may interact with. Object properties are directly accessible while methods are not, they do something rather than just get an internally stored value. The ToUpper method above does not contain any value within the parenthesis because it does not require any parameters, however, in some instances, you will need parameters for method calls. The following is an example of a method call with parameters:
Example
$fullname = "Johnny Tallman"
$newname = $fullname.replace("Tall", "Short")
write-output $newname
As you see, Tall was replaced with short. If the name were to be Tall Tallman, the method would change it to: Short Shortman.
A Few Last Words... Methods and objects are used quite frequently throughout PowerShell scripting, you can even create your own method and objects. We discuss this in future tutorials. Join us next time for additional Windows PowerShell tutorials!
lets see more in next sections