Richard's profileRichard Siddaway's BlogPhotosBlogLists Tools Help

Blog


    November 19

    Powershell and Objects

    PowerShell is .NET based.  .NET is object orientated. Therefore PowerShell is object based.  One of the first things we learn about PowerShell is that we are working with .NET objects.  Just what does that mean?

    Look at the list of cmdlets specifically designed to work directly with objects:

    Compare-Object
    ForEach-Object
    Group-Object
    Measure-Object
    New-Object
    Select-Object
    Sort-Object
    Tee-Object
    Where-Object

    Commonly the aliases foreach, group, select, sort and where are used for their respective cmdlets.  These are the ones that are used all of the time and everyone is happy with.  What about the others? When was the last time you used Tee-Object?

    Tee-Object enables you to store output in  a file or variable and display to the console (if tee-object terminates pipeline) or pass along the pipeline

    Consider

    Get-Process | Tee-Object -FilePath ptest.txt | Select name, handles

    Get-Content ptest.txt

    Will get the current processes, save the information in a file and then display the name and handles of the processes.  We can then view the contents of the file.

    Even more useful is this

    Get-Process | Tee-Object -Variable procs | select name, handles
    $procs
    $procs | select name, handles

    This time we put the information into a variable.  Note we don't use a $ on the variable name.

    We can then input $procs to the pipeline for further analysis.

    Any time you may need to dig further into some data think about using tee-object in your pipeline.  Could save a bit of effort.

     

    Share this post :

     

    Technorati Tags: ,

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1875.trak
    Weblogs that reference this entry
    • None