In this exercise, we work with the Windows PowerShell Environment provider.
Start Windows PowerShell.
Use the New-PSDrive cmdlet to create a drive mapping to the alias provider. The name of the new PSDrive will be al. The PSProvider is alias, and the root will be dot (.). This command is shown here:
new-PSDrive -name al -PSProvider alias -Root .
Change your working location to the new PSDrive you called al. To do this, use the sl alias for the Set-Location cmdlet. This is shown here:
SL al:\
Use the gci alias for the Get-ChildItem cmdlet, and pipeline the resulting object into the Sort-Object cmdlet by using the sort alias. Supply name as the property to sort on. This command is shown here:
GCI | Sort -Property name
Use the up arrow to retrieve the previous gci | sort -property name command and modify it to use a Where-Object cmdlet to return aliases only when the name is greater than the letter t. Use the where alias to avoid typing the entire name of the cmdlet. The resulting command is shown here:
GCI | sort -Property name | Where {$_.Name -gt "t"}c
Change your location back to the C:\ drive. To do this, use the sl alias and supply the C:\ argument. This is shown here:
SL C:\
Remove the PSDrive mapping for al. To do this, use the Remove-PSDrive cmdlet and supply the name of the PSDrive to remove. Note, this command does not want a trailing colon (:) or colon with backslash (:\). The command is shown here:
Remove-PSDrive al
Use the Get-PSDrive cmdlet to ensure the al drive was removed. This is shown here:
Get-PSDrive
Use the Get-Item cmdlet to obtain a listing of all the environment variables. Use the path argument and supply env:\ as the value. This is shown here:
Get-Item -Path env:\
Use the up arrow to retrieve the previous command, and pipeline the resulting object into the Get-Member cmdlet. This is shown here:
Get-Item -Path env:\ | Get-Member
The results from the previous command are shown here:
TypeName: System.Collections.Generic.Dictionary'2+ValueCollection[[System.St ring, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0 89],[System.Collections.DictionaryEntry, mscorlib, Version=2.0.0.0, Culture=neu tral, PublicKeyToken=b77a5c561934e089]] Name MemberType Definition ---- ---------- ---------- CopyTo Method System.Void CopyTo(DictionaryEntry[] array, Int32... Equals Method System.Boolean Equals(Object obj) GetEnumerator Method System.Collections.Generic.Dictionary`2+ValueColl... GetHashCode Method System.Int32 GetHashCode() GetType Method System.Type GetType() get_Count Method System.Int32 get_Count() ToString Method System.String ToString() PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=Env PSIsContainer NoteProperty System.Boolean PSIsContainer=True PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\En... PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvi... Count Property System.Int32 Count {get;}
Press the up arrow twice to return to the get-item -path env:\ command. Use the Home key to move your insertion point to the beginning of the line. Add a variable called $objEnv and use it to hold the object returned by the get-item -path env:\ command. The completed command is shown here:
$objEnv=Get-Item -Path env:\
From the listing of members of the environment object, find the count property. Use this property to print out the total number of environment variables. As you type $o, try to use Tab completion to avoid typing. Also try to use Tab completion as you type the c in count. The completed command is shown here:
$objEnv.Count
Examine the methods of the object returned by get-item -path env:\. Notice there is a Get_Count method. Let’s use that method. The code is shown here:
$objEnv.Get_count
When this code is executed, however, the results define the method rather than execute the Get_Count method. These results are shown here:
MemberType : Method OverloadDefinitions : {System.Int32 get_Count()} TypeNameOfValue : System.Management.Automation.PSMethod Value : System.Int32 get_Count() Name : get_Count IsInstance : True
To retrieve the actual number of environment variables, we need to use empty parentheses is at the end of the method. This is shown here:
$objEnv.Get_count()
If you want to know exactly what type of object you have contained in the $objEnv variable, you can use the GetType method, as shown here:
$objEnv.GetType()
This command returns the results shown here:
IsPublic IsSerial Name BaseType -------- -------- ---- -------- False True ValueCollection System.Object
This concludes this one step further exercise.