Calling static methods
Table of contents for Intro to PowerShell
- Getting PowerShell
- Basic PowerShell Concepts
- Commonly Used Cmdlets
- PowerShell Scripts and Profiles
- Using PowerShell types with commands and variables
- Error handling in PowerShell scripts
- Calling static methods
- $NestedPromptLevel
- Prompts in PowerShell
PowerShell is extremely flexible in what it allows you to do, even allowing you to call static methods on just about any class in just about any assembly. The problem I had was that I couldn’t remember what the syntax was. I seemed to recall that it included a double colon (::) but beyond that I was stumped. A short trip to Google led me to the Windows PowerShell blog. Here’s the syntax:
[full-type-name]::method-name(parameters)
For example:
PS C:\> [System.IO.Path]::GetDirectoryName("C:\Windows\Fred")
C:\WindowsNote that since there are no using statements in PowerShell like there are in C#, you must specify the full name, including all namespaces, for the type. Thus you can’t simply specify [Path] but instead must type [System.IO.Path]. Case is not important, however, so you could also type [system.io.path]::getdirectoryname and get the same result.
You can see what methods are available on a static class by using the -Static parameter to the Get-Member cmdlet:
PS C:\ > [System.IO.Path] | Get-Member -Static
TypeName: System.IO.Path
Name MemberType Definition
–––– –––––––––– ––––––––––
ChangeExtension Method static System.String ChangeExtension(String path, String extension)
Combine Method static System.String Combine(String path1, String path2)
Equals Method static System.Boolean Equals(Object objA, Object objB)
GetDirectoryName Method static System.String GetDirectoryName(String path)
GetExtension Method static System.String GetExtension(String path)
GetFileName Method static System.String GetFileName(String path)
GetFileNameWithoutExtension Method static System.String GetFileNameWithoutExtension(String path)
GetFullPath Method static System.String GetFullPath(String path)
GetInvalidFileNameChars Method static System.Char[] GetInvalidFileNameChars()
GetInvalidPathChars Method static System.Char[] GetInvalidPathChars()
GetPathRoot Method static System.String GetPathRoot(String path)
GetRandomFileName Method static System.String GetRandomFileName()
GetTempFileName Method static System.String GetTempFileName()
GetTempPath Method static System.String GetTempPath()
HasExtension Method static System.Boolean HasExtension(String path)
IsPathRooted Method static System.Boolean IsPathRooted(String path)
ReferenceEquals Method static System.Boolean ReferenceEquals(Object objA, Object objB)
AltDirectorySeparatorChar Property static System.Char AltDirectorySeparatorChar {get;set;}
DirectorySeparatorChar Property static System.Char DirectorySeparatorChar {get;set;}
InvalidPathChars Property static System.Char[] InvalidPathChars {get;set;}
PathSeparator Property static System.Char PathSeparator {get;set;}
VolumeSeparatorChar Property static System.Char VolumeSeparatorChar {get;set;}



Leave a Reply