Thursday, February 17, 2011

What can I do with windows powershell part3

Help and Information

The following task-based articles provide a brief introduction to getting help and information while using Windows PowerShell; these tasks include such things as listing Windows PowerShell verion information and using the Get-Member cmdlet to retrieve property and method information for an object. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.

Using the Get-Help Cmdlet

Getting Help

Windows PowerShell includes two basic types of help - command help and conceptual help - both of which can be accessed from the console using the Get-Help cmdlet. To retrieve a list of all available help topics simply type the following command:
Get-Help *
That will bring back a list similar to this:
Name                       Category                   Synopsis
----                       --------                   --------
ac                         Alias                      Add-Content
asnp                       Alias                      Add-PSSnapin
clc                        Alias                      Clear-Content
cli                        Alias                      Clear-Item
clp                        Alias                      Clear-ItemProperty
Once you have that list you can then get help on the specific topic by passing the topic name to Get-Help. For example, suppose you’d like more information about the Get-Member cmdlet. In that case, simply use that command:
Get-Help Get-Member
By default help appears in the console window as one long, scrolling topic. If you’d prefer to view help a single page at a time, then pipe the results of the Get-Help cmdlet to more:
Get-Help Get-Member | more

Using the Get-Command Cmdlet

Listing All the Windows PowerShell Cmdlets

Can’t remember the name of each and every Windows PowerShell cmdlet? Don’t worry about it; that’s one of the things Get-Command can help you with. Just type Get-Command without any additional parameters and you’ll get back a list of all the Windows PowerShell cmdlets:
Get-Command
That list will look something like this:
CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Add-Content                     Add-Content [-Path] <String[...
Cmdlet          Add-History                     Add-History [[-InputObject] ...
Cmdlet          Add-Member                      Add-Member [-MemberType] <PS...
Cmdlet          Add-PSSnapin                    Add-PSSnapin [-Name] <String...
Cmdlet          Clear-Content                   Clear-Content [-Path] <Strin...
Good point: that is a little hard to read, isn’t it? Try piping the results of Get-Command through the Format-List cmdlet (we’ve included the asterisk to indicate that we want back all the properties for each cmdlet):
Get-Command | Format-List *
That returns information similar to this for each cmdlet:
Name             : Write-Verbose
CommandType      : Cmdlet

DLL              : C:\WINDOWS\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.U
                   tility\1.0.9567.1__31bf3856ad364e35\Microsoft.PowerShell.Com
                   mands.Utility.dll
Verb             : Write
Noun             : Warning
HelpFile         : Microsoft.PowerShell.Commands.Utility.dll-Help.xml
PSSnapIn         : Microsoft.PowerShell.Utility
ImplementingType : Microsoft.PowerShell.Commands.WriteWarningCommand
ParameterSets    : {__AllParameterSets}
Definition       : Write-Warning [-Message] <String> [-Verbose] [-Debug] [-Erro
                   rAction <ActionPreference>] [-ErrorVariable <String>] [-OutV
                   ariable <String>] [-OutBuffer <Int32>]

Name             : Write-Warning
CommandType      : Cmdlet
Oh: all you really wanted was the cmdlet name? That’s easy enough; just use Select-Object to filter out all the properties except Name:
Get-Command | Select-Object name
Is this more of what you had in mind?
Name
----
Add-Content
Add-History
Add-Member
Add-PSSnapin
Clear-Content
Clear-Item
Clear-ItemProperty
Clear-Variable
Here’s a nifty way to use Get-Command. This command grabs the set of cmdlets installed on a computer and pipes that information to Get-Help. Get-Help dutifully retrieves the help topic for each cmdlet, then uses the Out-File cmdlet to save all those help topics to a file named C:\Scripts\Help.txt:
Get-Command | Get-Help | Out-File c:\scripts\help.txt
Run that command and, within a minute or so, you’ll have built yourself a document containing the on-line help available for each and every cmdlet.
Get-Command Aliases
  • gcm

Using the Get-Host Cmdlet

Listing Windows PowerShell Version Information

The Get-Host cmdlet returns information (such as the version number) for Windows PowerShell. For example:
Name             : ConsoleHost
Version          : 1.0.458.0
InstanceId       : 3a2087f5-ed5a-442d-9927-4367f2967116
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      :
To retrieve this information simply type Get-Host without any additional parameters:
Get-Host


Using the Get-Member Cmdlet

Listing the Properties and Methods of a Command or Object

Any time you’re writing scripts or working from the command line the biggest problem you face is this: how do I know what I can and cannot do? How do I know which properties and methods are available to me? How can I actually write a script or type a command-line command without having to memorize every object model found on MSDN?
One way to do that is to use the Windows PowerShell Get-Member cmdlet. Once you connect to an object you can pipe that object to Get-Member; in turn, Get-Member will enumerate the properties and methods of that object. For example, suppose you’d like to know which properties and methods are available for event logs. Assuming you know that Get-EventLog -list will retrieve an event log object, all you need to do is issue this command:
Get-EventLog -list | Get-Member
In turn, Windows PowerShell will report back data similar to this:
Name                      MemberType Definition
----                      ---------- ----------
add_Disposed              Method     System.Void add_Disposed(EventHandler v...
add_EntryWritten          Method     System.Void add_EntryWritten(EntryWritt...
BeginInit                 Method     System.Void BeginInit()
Clear                     Method     System.Void Clear()
Close                     Method     System.Void Close()
Interested in the properties of the WMI Win32_BIOS class? Then use this command, tacking on the -membertype parameter in order to limit returned data to properties:
Get-WmiObject win32_bios | Get-Member -membertype properties
And, yes, setting -membertype to methods returns just the methods.
Here’s an interesting use of Get-Member. Did you know that some of the properties returned by the Get-Process cmdlet have aliases? Well, you would if you ran this command, setting -membertype to AliasProperty:
Get-Process | Get-Member -membertype aliasproperty
Here’s what you get back:
Name    MemberType    Definition
----    ----------    ----------
Handles AliasProperty Handles = Handlecount
Name    AliasProperty Name = ProcessName
NPM     AliasProperty NPM = NonpagedSystemMemorySize
PM      AliasProperty PM = PagedMemorySize
VM      AliasProperty VM = VirtualMemorySize
WS      AliasProperty WS = WorkingSet
What does it mean for a property to have an alias? That simply means that the next time you work with the Get-Process cmdlet you can type NPM rather then NonpagedSystemMemorySize. For example:
Get-Process | Select-Object name, npm
And, yes, Get-Member does work with COM objects as well. Need to know the properties and methods of the FileSystemObject? All you have to do is use the New-Object cmdlet to create an instance of the FileSystemObject and then pipe that object to Get-Member:
New-Object -com scripting.filesystemobject | Get-Member
Here’s the type of information you’ll get back:
Name                MemberType Definition
----                ---------- ----------
BuildPath           Method     string BuildPath (string, string)
CopyFile            Method     void CopyFile (string, string, bool)
CopyFolder          Method     void CopyFolder (string, string, bool)
CreateFolder        Method     IFolder CreateFolder (string)
CreateTextFile      Method     ITextStream CreateTextFile (string, bool, bool)
DeleteFile          Method     void DeleteFile (string, bool)
DeleteFolder        Method     void DeleteFolder (string, bool)
DriveExists         Method     bool DriveExists (string)
FileExists          Method     bool FileExists (string)
FolderExists        Method     bool FolderExists (string)

Get-Member Aliases
  • gm
Using the Get-PSProvider Cmdlet

Listing Your Installed Windows PowerShell Providers

Among other things, Windows PowerShell providers determine which data stores you can navigate through. For example, you can use the Get-ChildItem command to retrieve a list of all the files found in a folder; that’s because a provider exists for the file system. You can also use Get-ChildItem to retrieve a list of all the subkeys found in a registry key. Why? You got it: because a provider exists for the registry.
To determine the providers available for you, simply call the Get-PSProvider cmdlet without any parameters:
Get-PSProvider
What you’ll get back is - surprise, surprise - information about your Windows PowerShell providers:
Name                 Capabilities                  Drives
----                 ------------                  ------
Alias                ShouldProcess                 {Alias}
Environment          ShouldProcess                 {Env}
FileSystem           Filter, ShouldProcess         {C, D, E}
Function             ShouldProcess                 {Function}
Registry             ShouldProcess                 {HKLM, HKCU}
Variable             ShouldProcess                 {Variable}
Certificate          ShouldProcess                 {cert}

No comments:

Post a Comment