none
How to use methods of any object in PowerShell

    Question

  • Hello PowerShell Expert,

    I want to learn how to use different methods available in any object in PowerShell . 

    I can get the methods easily by using get-member , for ex-   Get-ChildItem |get-Member 

    But , I want  , how to use these methods  ?

    where can find proper syntax of these method  ?

    Any idea  Please ?

    Thanks in Advance.

    Friday, August 23, 2013 3:03 PM

Answers

  • For the most detailed information, look up the classes on MSDN.  When you look at Get-Member, at the top of the output you'll see something like "TypeName: System.IO.FileInfo".  If you search online for System.IO.FileInfo , you'll find this link: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

    You can also get a fair amount of information from within PowerShell itself.  Keeping with the FileInfo example, you could type (Get-Item .\SomeFile.txt | Get-Member -MemberType Method | Format-Table -Wrap) at a console (assuming SomeFile.txt exists in your current directory).  The Definition column gives you the signatures of each method (including its overloads), but doesn't give you the background information and text descriptions that you would get by looking at MSDN.

    Friday, August 23, 2013 3:44 PM

All replies

  • For the most detailed information, look up the classes on MSDN.  When you look at Get-Member, at the top of the output you'll see something like "TypeName: System.IO.FileInfo".  If you search online for System.IO.FileInfo , you'll find this link: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

    You can also get a fair amount of information from within PowerShell itself.  Keeping with the FileInfo example, you could type (Get-Item .\SomeFile.txt | Get-Member -MemberType Method | Format-Table -Wrap) at a console (assuming SomeFile.txt exists in your current directory).  The Definition column gives you the signatures of each method (including its overloads), but doesn't give you the background information and text descriptions that you would get by looking at MSDN.

    Friday, August 23, 2013 3:44 PM
  • Hi,

    I can just double what David mentioned. To look up the info on MSDN there is a great PowerShell function Get-MSDNInfo from Shay Levi.

    http://gallery.technet.microsoft.com/ScriptCenter/en-us/31bb27db-ab0e-4fa7-a6a9-2faedb1bae9d

    With that you can do something like this:

    (get-childitem)[0] | get-msdninfo -membertype method
    
      TypeName: System.IO.DirectoryInfo
    
    1. Create
    2. CreateObjRef
    3. CreateSubdirectory
    4. Delete
    5. EnumerateDirectories
    6. EnumerateFiles
    7. EnumerateFileSystemInfos
    8. Equals
    9. GetAccessControl
    10. GetDirectories
    11. GetFiles
    12. GetFileSystemInfos
    13. GetHashCode
    14. GetLifetimeService
    15. GetObjectData
    16. GetType
    17. InitializeLifetimeService
    18. MoveTo
    19. Refresh
    20. SetAccessControl
    21. ToString
    
    Enter the Method item number ( Between 1 and 21. Press CTRL+C to cancel): 4
    
    #this will then automatically open the related MSDN website

    The function has several other options that help you to explore any .net object.


    • Edited by Dirk_74 Friday, August 23, 2013 3:52 PM typo
    Friday, August 23, 2013 3:51 PM
  • For the most detailed information, look up the classes on MSDN.  When you look at Get-Member, at the top of the output you'll see something like "TypeName: System.IO.FileInfo".  If you search online for System.IO.FileInfo , you'll find this link: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

    You can also get a fair amount of information from within PowerShell itself.  Keeping with the FileInfo example, you could type (Get-Item .\SomeFile.txt | Get-Member -MemberType Method | Format-Table -Wrap) at a console (assuming SomeFile.txt exists in your current directory).  The Definition column gives you the signatures of each method (including its overloads), but doesn't give you the background information and text descriptions that you would get by looking at MSDN.

    Thank you David,

    when I look up the Class on MSDN , there it is showing  for C#, VB and C++ language  but not for PowerShell.

    how can I discover the  proper syntax using with PowerShell ?

    Friday, August 23, 2013 5:25 PM
  • Hi,

    I can just double what David mentioned. To look up the info on MSDN there is a great PowerShell function Get-MSDNInfo from Shay Levi.

    http://gallery.technet.microsoft.com/ScriptCenter/en-us/31bb27db-ab0e-4fa7-a6a9-2faedb1bae9d

    With that you can do something like this:

    (get-childitem)[0] | get-msdninfo -membertype method
    
      TypeName: System.IO.DirectoryInfo
    
    1. Create
    2. CreateObjRef
    3. CreateSubdirectory
    4. Delete
    5. EnumerateDirectories
    6. EnumerateFiles
    7. EnumerateFileSystemInfos
    8. Equals
    9. GetAccessControl
    10. GetDirectories
    11. GetFiles
    12. GetFileSystemInfos
    13. GetHashCode
    14. GetLifetimeService
    15. GetObjectData
    16. GetType
    17. InitializeLifetimeService
    18. MoveTo
    19. Refresh
    20. SetAccessControl
    21. ToString
    
    Enter the Method item number ( Between 1 and 21. Press CTRL+C to cancel): 4
    
    #this will then automatically open the related MSDN website

    The function has several other options that help you to explore any .net object.


    Thank you Dirk,

    This is also referring me to MSDN  where Classes and its methods are listed for C#,C++ and VB but not for PowerShell .

     

    Friday, August 23, 2013 5:28 PM
  • Most of the time, the syntax is almost identical to C#, except where "out" or "ref" parameters are required, or when you're calling static methods.  Types in PowerShell are placed in square brackets, and to access a static member of a class, you use two colons instead of a period (similar to old C++ notation).  For "ref" or "out" parameters, you put [ref] in front of the PowerShell variable.

    Other than that, the main differences will be that PowerShell variables start with a $ character, and C# statements must end with a semicolon (while that's optional in PowerShell).  Here are a couple of examples:

    # C# version:
    acl = file.GetAccessControl();

    # PowerShell version: $acl = $file.GetAccessControl()

    Here's an example of a static method that uses an out parameter in C#, and how to do that in PowerShell:

    # C# version:
    
    int number = 0;
    string s = "12345";
    
    if (Int32.TryParse(s, out number))
    {
        Console.WriteLine("Yay!");
    }
    
    # PowerShell version:
    
    $number = 0
    $s = "12345"
    
    if ([Int32]::TryParse($s, [ref] $number))
    {
        Write-Host "Yay!"
    }
    

    Friday, August 23, 2013 5:37 PM
  • Hi,

    looks like it is very difficult to learn methods of PowerShell  for non-developer guy .... isn't  it ?

    Syntax for PowerShell methods should be mentioned in MSDN like other language .

    I was just looking for using methods for AD ,Exchange and general Windows PowerShell cmdlets .

    Any other options  or documentation ?

    Friday, August 23, 2013 6:13 PM
  • There's a bit of a learning curve when it comes to PowerShell, if you're not familiar with the .NET Framework.  If you're learning to write PowerShell scripts, you're basically becoming a developer on some level.

    If the PowerShell help files aren't enough (such as about_Objects, about_Methods, and about_Properties), there are plenty of books and classes available for learning PowerShell from the ground up.  You can search around for these, but here's a reasonable place to start:  http://powershell.org/wp/2012/04/21/powershell-book-recommendations/


    Friday, August 23, 2013 6:28 PM
  • Here's a link to my favorite command reference site:

    http://ss64.com/ps/

    It usually has pretty good examples as well.


    Don't retire TechNet!

    Friday, August 23, 2013 6:34 PM
  • Thanks a lot David for valuable suggestion .

    That's right I am learning PowerShell script for Managing Exchange and AD administrative task .

    I will wait for some other's valuable suggestion to learn methods in PowerShell , then I'll mark your post and other's post as Answer.

    Thanks again !!

    Friday, August 23, 2013 6:42 PM