How can I find what Static Classes and Methods there is available in PowerShell 2.0?
-
You mean commands you can execute? See Get-Command technet.microsoft.com/en-us/library/ee176842.aspx Or do you mean static .Net classes and it's static methods? These are not Powershell specific.Lars Truijens– Lars Truijens2011-09-20 18:23:45 +00:00Commented Sep 20, 2011 at 18:23
-
Yes, I meant .Net classes and static methods that you can use with PowerShell. I don't know .Net, so it would be nice to see a list of available classes and methods one can use via PowerShell.jrara– jrara2011-09-20 18:27:39 +00:00Commented Sep 20, 2011 at 18:27
-
2You can use all .Net classes and they don't even need to be static either. See technet.microsoft.com/en-us/library/dd347574.aspx I think you are better off browsing msdn.microsoft.com/en-us/library/gg145045(v=VS.100).aspxLars Truijens– Lars Truijens2011-09-20 18:30:56 +00:00Commented Sep 20, 2011 at 18:30
6 Answers
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
4 Comments
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
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
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
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)