Operate the data (Intermediate)
Data Operations are critical routine administrative tasks and, with the release of Windows PowerShell Version 3.0, we have made some improvements regarding the handling of data in the console.
Getting ready
There are a few new operators introduced in PowerShell Version 3.0. For example, the In and NotIn operators.
How to do it...
Try executing the following code:
The following command checks the availability of
1in the reference set and returnsTruein this case:PS C :\> 1 -In (1,2,3) True
The following command checks for an exact counter match for the
Adminkeyword in the reference set and returnsTruein this case:PS C :\> "Admin" -NotIn "Administrator" True
How it works...
The following table mentions the syntaxes for these operators:
|
Operator |
Syntax |
Description |
|---|---|---|
|
|
|
This operator returns a Boolean value as output. If any test value is present in the reference values set, it returns |
|
|
|
This operator also returns a Boolean value as output. If any test value is not present in the reference values set, it returns |
There's more…
There are a few new parameters introduced with the following CMDLETs:
Get-Content
The Get-Content CMDLET retrieves content from a specified file.
-Tail <Int32>: To retrieve the number of lines from the file, use theTailparameter with theGet-ContentCMDLET. It retrieves the number of lines supplied to this parameter. For example:PS C :\> Get-Content -Path C:\PSTest.txt -Tail 10The preceding command statement retrieves the last 10 lines from
C:\PSTest.txt.
Tip
You can use the –Last instead of the Tail parameter; it is an alias of this parameter.
Tee-Object
The Tee-Object CMDLET stores output in a file or variable and throws to the pipeline.
-Append [<SwitchParameter>]: If any file already exists and you try to supply it withTee-Object, it overrides the content by default. To avoid this, you can use the–Appendparameter with theTee-ObjectCMDLET. For example:PS C:\>Get-ChildItem -Path C:\PSBooks -Recurse | Tee-Object -File C:\BookList.txt –AppendThe preceding command statement gets the list of PowerShell books and appends it to
BookList.txt.