Finding out how to use Methods in Get-Member

I have been wrapping my head around powershell and finally got the importance of Get-Member (go figure).

I can get the list of properties and methods but I was wondering if there was any way to know how to use the methods/property like get-help with cmdlets?

Thanks for your help.

Wei-Yen Tan,

You can do something like this:

Get-Service | gm -MemberType Properties

This will show you the properties get-service

Get-ChildItem | gm -MemberType Method

This will show you the Methods for Get-Childitem

Also, you should check out Learn Windows PowerShell 3 in a Month of Lunches by Don Jones.

Wei-Yen Tan,
The usage information for each method is actually in the definitions property for the method. Sometimes it is hard to read because the screen cuts it off because it defaults to trying to show it in a table view. If you Pipe it to a Format-List you can see the Definitions property better.

Get-ChildItem | get-member -MemberType method | Format-List

PS C:\> Get-ChildItem | get-member -MemberType method | Format-List


TypeName   : System.IO.DirectoryInfo
Name       : Create
MemberType : Method
Definition : void Create(), void Create(System.Security.AccessControl.DirectorySecurity directorySecurity)

TypeName   : System.IO.DirectoryInfo
Name       : CreateObjRef
MemberType : Method
Definition : System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)

TypeName   : System.IO.DirectoryInfo
Name       : CreateSubdirectory
MemberType : Method
Definition : System.IO.DirectoryInfo CreateSubdirectory(string path), System.IO.DirectoryInfo
             CreateSubdirectory(string path, System.Security.AccessControl.DirectorySecurity directorySecurity)

TypeName   : System.IO.DirectoryInfo
Name       : Delete
MemberType : Method
Definition : void Delete(), void Delete(bool recursive)

TypeName   : System.IO.DirectoryInfo
Name       : EnumerateDirectories
MemberType : Method
Definition : System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo] EnumerateDirectories(),
             System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo] EnumerateDirectories(string
             searchPattern), System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo]
             EnumerateDirectories(string searchPattern, System.IO.SearchOption searchOption)

TypeName   : System.IO.DirectoryInfo
Name       : EnumerateFiles
MemberType : Method
Definition : System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles(),
             System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles(string searchPattern),
             System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles(string searchPattern,
             System.IO.SearchOption searchOption)

TypeName   : System.IO.DirectoryInfo
Name       : EnumerateFileSystemInfos
MemberType : Method
Definition : System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo] EnumerateFileSystemInfos(),
             System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo] EnumerateFileSystemInfos(string
             searchPattern), System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo]
             EnumerateFileSystemInfos(string searchPattern, System.IO.SearchOption searchOption)

TypeName   : System.IO.DirectoryInfo
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)

TypeName   : System.IO.DirectoryInfo
Name       : GetAccessControl
MemberType : Method
Definition : System.Security.AccessControl.DirectorySecurity GetAccessControl(),
             System.Security.AccessControl.DirectorySecurity
             GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections)

TypeName   : System.IO.DirectoryInfo
Name       : GetDirectories
MemberType : Method
Definition : System.IO.DirectoryInfo[] GetDirectories(), System.IO.DirectoryInfo[] GetDirectories(string
             searchPattern), System.IO.DirectoryInfo[] GetDirectories(string searchPattern, System.IO.SearchOption
             searchOption)

TypeName   : System.IO.DirectoryInfo
Name       : GetFiles
MemberType : Method
Definition : System.IO.FileInfo[] GetFiles(string searchPattern), System.IO.FileInfo[] GetFiles(string searchPattern,
             System.IO.SearchOption searchOption), System.IO.FileInfo[] GetFiles()

TypeName   : System.IO.DirectoryInfo
Name       : GetFileSystemInfos
MemberType : Method
Definition : System.IO.FileSystemInfo[] GetFileSystemInfos(string searchPattern), System.IO.FileSystemInfo[]
             GetFileSystemInfos(string searchPattern, System.IO.SearchOption searchOption), System.IO.FileSystemInfo[]
             GetFileSystemInfos()

TypeName   : System.IO.DirectoryInfo
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()

TypeName   : System.IO.DirectoryInfo
Name       : GetLifetimeService
MemberType : Method
Definition : System.Object GetLifetimeService()

TypeName   : System.IO.DirectoryInfo
Name       : GetObjectData
MemberType : Method
Definition : void GetObjectData(System.Runtime.Serialization.SerializationInfo info,
             System.Runtime.Serialization.StreamingContext context), void
             ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info,
             System.Runtime.Serialization.StreamingContext context)

TypeName   : System.IO.DirectoryInfo
Name       : GetType
MemberType : Method
Definition : type GetType()

TypeName   : System.IO.DirectoryInfo
Name       : InitializeLifetimeService
MemberType : Method
Definition : System.Object InitializeLifetimeService()

TypeName   : System.IO.DirectoryInfo
Name       : MoveTo
MemberType : Method
Definition : void MoveTo(string destDirName)

TypeName   : System.IO.DirectoryInfo
Name       : Refresh
MemberType : Method
Definition : void Refresh()

TypeName   : System.IO.DirectoryInfo
Name       : SetAccessControl
MemberType : Method
Definition : void SetAccessControl(System.Security.AccessControl.DirectorySecurity directorySecurity)

TypeName   : System.IO.DirectoryInfo
Name       : ToString
MemberType : Method
Definition : string ToString()

TypeName   : System.IO.FileInfo
Name       : AppendText
MemberType : Method
Definition : System.IO.StreamWriter AppendText()

TypeName   : System.IO.FileInfo
Name       : CopyTo
MemberType : Method
Definition : System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool
             overwrite)

TypeName   : System.IO.FileInfo
Name       : Create
MemberType : Method
Definition : System.IO.FileStream Create()

TypeName   : System.IO.FileInfo
Name       : CreateObjRef
MemberType : Method
Definition : System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)

TypeName   : System.IO.FileInfo
Name       : CreateText
MemberType : Method
Definition : System.IO.StreamWriter CreateText()

TypeName   : System.IO.FileInfo
Name       : Decrypt
MemberType : Method
Definition : void Decrypt()

TypeName   : System.IO.FileInfo
Name       : Delete
MemberType : Method
Definition : void Delete()

TypeName   : System.IO.FileInfo
Name       : Encrypt
MemberType : Method
Definition : void Encrypt()

TypeName   : System.IO.FileInfo
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)

TypeName   : System.IO.FileInfo
Name       : GetAccessControl
MemberType : Method
Definition : System.Security.AccessControl.FileSecurity GetAccessControl(), System.Security.AccessControl.FileSecurity
             GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections)

TypeName   : System.IO.FileInfo
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()

TypeName   : System.IO.FileInfo
Name       : GetLifetimeService
MemberType : Method
Definition : System.Object GetLifetimeService()

TypeName   : System.IO.FileInfo
Name       : GetObjectData
MemberType : Method
Definition : void GetObjectData(System.Runtime.Serialization.SerializationInfo info,
             System.Runtime.Serialization.StreamingContext context), void
             ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info,
             System.Runtime.Serialization.StreamingContext context)

TypeName   : System.IO.FileInfo
Name       : GetType
MemberType : Method
Definition : type GetType()

TypeName   : System.IO.FileInfo
Name       : InitializeLifetimeService
MemberType : Method
Definition : System.Object InitializeLifetimeService()

TypeName   : System.IO.FileInfo
Name       : MoveTo
MemberType : Method
Definition : void MoveTo(string destFileName)

TypeName   : System.IO.FileInfo
Name       : Open
MemberType : Method
Definition : System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Open(System.IO.FileMode mode,
             System.IO.FileAccess access), System.IO.FileStream Open(System.IO.FileMode mode, System.IO.FileAccess
             access, System.IO.FileShare share)

TypeName   : System.IO.FileInfo
Name       : OpenRead
MemberType : Method
Definition : System.IO.FileStream OpenRead()

TypeName   : System.IO.FileInfo
Name       : OpenText
MemberType : Method
Definition : System.IO.StreamReader OpenText()

TypeName   : System.IO.FileInfo
Name       : OpenWrite
MemberType : Method
Definition : System.IO.FileStream OpenWrite()

TypeName   : System.IO.FileInfo
Name       : Refresh
MemberType : Method
Definition : void Refresh()

TypeName   : System.IO.FileInfo
Name       : Replace
MemberType : Method
Definition : System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName),
             System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool
             ignoreMetadataErrors)

TypeName   : System.IO.FileInfo
Name       : SetAccessControl
MemberType : Method
Definition : void SetAccessControl(System.Security.AccessControl.FileSecurity fileSecurity)

TypeName   : System.IO.FileInfo
Name       : ToString
MemberType : Method
Definition : string ToString()

THank you both for your replies.

Curtis, Is there any examples as well in the cmdlets some place on examples on how we can use the method. The definitions look a bit cryptic…or is this something we get used to over time? :slight_smile:

Well, you will likely get used to the format over time, however, PowerShell is built on the .Net framework. You can see the object type from get-member and use that to look up that .Net object and it’s associated methods on the web. For example, the first method in the list above is the “Create” method. You can also see this method comes from the “System.IO.DirectoryInfo” type. A quick google for “System.IO.DirectoryInfo Methods” returns the MSDN site showing all of its methods and usage including the “Create” method. “https://msdn.microsoft.com/en-us/library/system.io.directoryinfo_methods(v=vs.110).aspx

Thank you very much Curtis for this. I just needed a reference to go off. :slight_smile:

Now to go back to scripting …