Basic Powershell examples, couple useful commands

I’ve been bitten by the Powershell bug.  My scripting background tells me I should really learn this tool.  Since I’ve been writing console applications for a few years, mostly to do WMI and related Web service calls.  This should be fairly easy in Powershell.  As with anything, learning the syntax and how things are done is a bit frustrating at first.  There are tons of examples available, however if you are like me, you need to figure it out the hard way. Once you get the swing of it, things should be pretty straight forward.

After figuring out a couple of examples, which are posted below, I’m starting to get the swing of it.  There is a lot though I don’t understand but this will come in time. I miss intellisense that is built into Visual Studio.   The get-member cmdlet is useful in helping figure out which methods / properties are available to use.  Another command I like is man.  A few years back, I got into FreeBSD and used the ‘man’ command all the time.  Powershell has this available and it seems natural.  This is a basic blog, but helps me learn Powershell and share some a couple examples for future reference.  Happy Scripting!

Using MAN to pipe out information on get-member cmdlet

Type PS c:>man get-member

This is what is displayed,  a nice short version of help.

NAME
    Get-Member
   
SYNOPSIS
    Gets information about objects or collections of objects.
   
   
SYNTAX
    Get-Member [[-name] <string[]>] [-inputObject <psobject>] [-memberType {<AliasProperty> | <CodeProperty> | <Propert
    y> | <NoteProperty> | <ScriptProperty> | <Properties> | <PropertySet> | <Method> | <CodeMethod> | <ScriptMethod> |
    <Methods> | <ParameterizedProperty> | <MemberSet> | <All>}] [-static] [<CommonParameters>]
   
   
DETAILED DESCRIPTION
    Gets information about the members of objects. Get-Member can accept input from the pipeline or as the value of the
     InputObject parameter. You can use the MemberType parameter to specify the type of members you want information ab
    out.
   
    If you pipeline input to Get-Member, it outputs a MemberDefinition object for each distinct type of input object. 
    For example, if you pipe the output of Get-ChildItem to Get-Member in a directory that includes at least one subdir
    ectory and one file, it returns two MemberDefinition objects. One includes information about the FileInfo object an
    d the other includes information about the DirectoryInfo object. Get-Member outputs only two MemberDefinition objec
    ts, regardless of how many files or subdirectories are in the directory.
   
    The output of Get-Member is different if you supply input by using the InputObject parameter. In that case, Get-Mem
    ber returns a single MemberDefinition object that represents either the single input object or the collection class
     that contains the set of input objects.
   
    To retrieve information about static members, you must specify the Static parameter.
   

RELATED LINKS
    Add-Member
    Get-Help
    Get-Command
    Get-PSDrive

REMARKS
    For more information, type: "get-help Get-Member -detailed".
    For technical information, type: "get-help Get-Member -full".

Create a new object at the command line
$sw = new-object System.IO.StreamWriter("c:tempss.txt")

Type this to find out all methods available
$sw | get-member -memberType methods

PS C:temp> $sw | get-member -membertype methods

   TypeName: System.IO.StreamWriter

Name                      MemberType Definition
—-                      ———- ———-
Close                     Method     System.Void Close()
CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType)
Dispose                   Method     System.Void Dispose()
Equals                    Method     System.Boolean Equals(Object obj)
Flush                     Method     System.Void Flush()
GetHashCode               Method     System.Int32 GetHashCode()
GetLifetimeService        Method     System.Object GetLifetimeService()
GetType                   Method     System.Type GetType()
get_AutoFlush             Method     System.Boolean get_AutoFlush()
get_BaseStream            Method     System.IO.Stream get_BaseStream()
get_Encoding              Method     System.Text.Encoding get_Encoding()
get_FormatProvider        Method     System.IFormatProvider get_FormatProvider()
get_NewLine               Method     System.String get_NewLine()
InitializeLifetimeService Method     System.Object InitializeLifetimeService()
set_AutoFlush             Method     System.Void set_AutoFlush(Boolean value)
set_NewLine               Method     System.Void set_NewLine(String value)
ToString                  Method     System.String ToString()
Write                     Method     System.Void Write(Char value), System.Void Write(Char[] buffer), System.Void Wr..
WriteLine                 Method     System.Void WriteLine(), System.Void WriteLine(Char value), System.Void WriteLi..

‘Create a web request
$request = [System.Net.WebRequest]::Create("http://www.iislogs.com/testlink.aspx")
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = new-object System.IO.StreamReader $requestStream
new-variable db
$db = $readStream.ReadToEnd()
$readStream.Close()
$response.Close()

‘Create a new file and write the contents of that to a file
$sw = new-object system.IO.StreamWriter("c:tempss2.txt")
$sw.writeline($db)
$sw.close()
get-content "c:tempss.txt"

Take care,

Steve Schofield
Microsoft MVP – IIS