2

How can I find what Static Classes and Methods there is available in PowerShell 2.0?

3

6 Answers 6

7

You can use any .NET types and their static methods from PowerShell. To enumerate all that are currently loaded into your AppDomain, you can do:

 [AppDomain]::CurrentDomain.GetAssemblies() | foreach { $_.GetTypes() } | foreach { $_.GetMethods() } | where { $_.IsStatic } | select DeclaringType, Name | format-table

Remember, you are not limited to static methods, you can also instantiate the types using new-object and call instance methods. You can use get-member on an instance to get the methods on a type.

Also, if you want to list your available CmdLets, just invoke:

Get-Command
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thanks! Is there a way to remove duplicates from this list?
I don't think it has duplicates, but perhaps subclasses report the same static method twice (Do you have an example of a duplicate ?)
When I run this on my PowerShell console, I get eg. System.Convert ToDateTime System.Convert ToDateTime System.Convert ToString System.Convert ToString
Ah, that's different overloads of the methods. You can either filter by name (if you don't care about the overloads), or you could include parameter information in the list - depends on what you need it for.
4

To get the static members of a type or object, pipe it to Get-Member and specify the Static switch:

[math] | Get-Member -Static

Comments

1

Mr driis, you are the man !!! Totally made my day !

I just took the liberty to modify it a little bit so it returns the whole list without duplicates:

PS C:\Users\Administrator> [AppDomain]::CurrentDomain.GetAssemblies() | foreach { $_.GetTypes() } | foreach { $_.GetMethods() } | where { $_.IsStatic } | select DeclaringType | Out-File assemblies.txt

and then read the assemblies.txt file but only get unique rows :

cat .\assemblies.txt Get-Unique

1 Comment

Thanks i also could create unique file cat .\assemblies.txt Get-Unique | Out-File unique.txt
0

You have the classes ( static or otherwise ) from .NET framework.

Once you have the class, you can use Get-Member:

[Environment] | Get-Member

PS: "Windows PowerShell Cookbook" by Lee Holmes has an Appendix which lists some useful classes, from Powershell / SysAdmin point of view. That list( and the book) is very useful.

Comments

0

The following code produces files in the Downloads folder. Each file contains the static members of the public exported types of all namespaces of the assemblies in the current domain.

Add-Type -Assembly System.Collections
Add-Type -Assembly System.Management.Automation
$StaticMemberList = New-Object -TypeName 'System.Collections.Generic.List[PSCustomObject]'
$TempList = New-Object -TypeName 'System.Collections.Generic.List[char]'
[PSCustomObject]$StaticMemberObj = $null
[string]$TypeFullName = [string]::Empty
[string]$MemberType   = [string]::Empty
[string]$NameSpaceName    = [string]::Empty
[string]$AssemblyQualifiedName = [string]::Empty
[string]$Str = [string]::Empty
[string]$TrimmedStr = [string]::Empty
[int]$ParenthesisCount = 0
[int]$SquareBracketCount = 0
$ShellApplication = New-Object -ComObject Shell.Application
[string]$ShellDownloads = $ShellApplication.NameSpace('shell:Downloads').Self.Path
$NameSpaceGroups = [AppDomain]::CurrentDomain.GetAssemblies().GetExportedTypes()|Where-Object {$_.IsPublic}|Select-Object -Property FullName,NameSpace|Group-Object -Property NameSpace
foreach( $NameSpace in $NameSpaceGroups ) {
    $TypeList = $NameSpace.Group  # $TypeList has type collection
    $NameSpaceName = $NameSpace.Name
    foreach( $Type in $TypeList ) {
        $TypeFullName = $Type.FullName
        $AssemblyQualifiedName = Invoke-Expression -Command $("["+$TypeFullName+"].AssemblyQualifiedName")
        $StaticMembers = Invoke-Expression -Command $("["+$TypeFullName+"]|Get-Member -Static")
        foreach( $StaticMember in $StaticMembers ) {
            $MemberType = [enum]::GetName([System.Management.Automation.PSMemberTypes], $StaticMember.MemberType)
            $Definitions = $StaticMember.Definition
            $DefinitionsCharArr = $Definitions.ToCharArray() # getenumerator()
            $ParenthesisCount = 0
            $SquareBracketCount = 0
            foreach( $c in $DefinitionsCharArr ) {
                if( '(' -eq $c ) { $ParenthesisCount++ }
                if( ')' -eq $c ) { $ParenthesisCount-- }
                if( '[' -eq $c ) { $SquareBracketCount++ }
                if( ']' -eq $c ) { $SquareBracketCount-- }
                if( ',' -eq $c ) {
                    if( (0 -eq $ParenthesisCount) -and (0 -eq $SquareBracketCount) ) {
                        $Str = $TempList -join ''
                        $TrimmedStr = $Str.Trim()
                        $StaticMemberObj = New-Object -Type "PSCustomObject"
                        Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name FullName              -Value $TypeFullName
                        Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name MemberType            -Value $MemberType
                        Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name NameSpace             -Value $NameSpaceName
                        Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name AssemblyQualifiedName -Value $AssemblyQualifiedName
                        Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name Definition            -Value $TrimmedStr
                        $StaticMemberList.Add($StaticMemberObj)
                        $TempList.Clear()
                    } #end if( 0
                    else {
                        $TempList.Add($c)
                    }
                } #end if( ','
                else {
                    $TempList.Add($c)
                } #end else
            } #end foreach( $c in 
            $Str = $($TempList -join '')
            $TrimmedStr = $Str.Trim()
            $StaticMemberObj = New-Object -Type "PSCustomObject"
            Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name FullName              -Value $TypeFullName
            Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name MemberType            -Value $MemberType
            Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name NameSpace             -Value $NameSpaceName
            Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name AssemblyQualifiedName -Value $AssemblyQualifiedName
            Add-Member -InputObject $StaticMemberObj -MemberType NoteProperty -Name Definition            -Value $TrimmedStr
            $StaticMemberList.Add($StaticMemberObj)
            $TempList.Clear()
        } #end foreach( $StaticMember in 
    } #end foreach( $Type in 
    $Temporary = ConvertTo-JSON -InputObject $StaticMemberList -Depth 50
    Out-File -FilePath $($ShellDownloads+"\StaticMemberList "+$NameSpaceName+".txt") -InputObject $Temporary
    $StaticMemberList.Clear()
} #end foreach( $NameSpace in 

Comments

0

I hope you're not still using Powershell 2.0. Tab completion is your friend, especially if the tab lists all completions at once. Also running the method without the parentheses shows the overload declarations.

set-psreadlineoption -editmode emacs  # tab shows all completions 
set-PSReadLineKeyHandler ctrl+v paste # but changes control-v

#Set-PSReadLineKeyHandler tab complete  # or

[arraylis # press tab
[System.Collections.ArrayList

# static methods
[System.Collections.ArrayList]:: # press tab
Adapter          FixedSize        ReadOnly         Repeat
Equals           new              ReferenceEquals  Synchronized

# instance methods
([System.Collections.ArrayList]$array). # press tab
Capacity        SyncRoot        Clone           GetEnumerator   Insert          RemoveRange     ToString
Count           Add             Contains        GetHashCode     InsertRange     Reverse         TrimToSize
IsFixedSize     AddRange        CopyTo          GetRange        LastIndexOf     SetRange        Where
IsReadOnly      BinarySearch    Equals          GetType         Remove          Sort
IsSynchronized  Clear           ForEach         IndexOf         RemoveAt        ToArray

[System.Collections.ArrayList]::equals

OverloadDefinitions
-------------------
static bool Equals(System.Object objA, System.Object objB)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.