SoFunction
Updated on 2025-03-04

Practical static methods of .Net that can be used in Powershell

All versions are supported.

Powershell can use .net static methods, such as the following methods you often use:

Copy the codeThe code is as follows:

[Math]::Round(7.9)
 
[Convert]::ToString(576255753217, 8)
 
[Guid]::NewGuid()
 
[]::GetHostByName('schulung12')
 
[]::GetExtension('c:\')
 
[]::ChangeExtension('c:\', 'bak')

To get more methods, you can delete the code after square brackets and add two colons. At this time, all optional methods and properties of this class will pop up in Powershell_ISE. In the Powershell console, you can also use TAB to get the selection method properties.

Of course, you can also view all its types through the pipe character:

Copy the codeThe code is as follows:

PS> [Math] | Get-Member -MemberType *Method -Static


   TypeName:

Name            MemberType Definition                                                                                    
----            ---------- ----------                                                                                    
Abs             Method     static sbyte Abs(sbyte value), static int16 Abs(int16 value), static int Abs(int value), sta...
Acos            Method     static double Acos(double d)          
Asin            Method     static double Asin(double d)           
Atan            Method     static double Atan(double d)           
Atan2           Method     static double Atan2(double y, double x)
BigMul          Method     static long BigMul(int a, int b)  
Ceiling         Method     static decimal Ceiling(decimal d), static double Ceiling(double a)
Cos             Method     static double Cos(double d)
Cosh            Method     static double Cosh(double value                                                            
DivRem          Method     static int DivRem(int a, int b, [ref] int result), static long DivRem(long a, long b, [ref] ...
Equals          Method     static bool Equals( objA, objB)
Exp             Method     static double Exp(double d)             
Floor           Method     static decimal Floor(decimal d), static double Floor(double d)
IEEERemainder   Method     static double IEEERemainder(double x, double y)
Log             Method     static double Log(double d), static double Log(double a, double newBase)
Log10           Method     static double Log10(double d)
Max             Method     static sbyte Max(sbyte val1, sbyte val2), static byte Max(byte val1, byte val2), static int1...
Min             Method     static sbyte Min(sbyte val1, sbyte val2), static byte Min(byte val1, byte val2), static int1...
Pow             Method     static double Pow(double x, double y)
ReferenceEquals Method     static bool ReferenceEquals( objA, objB)
Round           Method     static double Round(double a), static double Round(double value, int digits), static double ...
Sign            Method     static int Sign(sbyte value), static int Sign(int16 value), static int Sign(int value), stat...
Sin             Method     static double Sin(double a)
Sinh            Method     static double Sinh(double value)  
Sqrt            Method     static double Sqrt(double d)
Tan             Method     static double Tan(double a)
Tanh            Method     static double Tanh(double value)
Truncate        Method     static decimal Truncate(decimal d), static double Truncate(double d)

To view the parameters of the method, omit its brackets:

Copy the codeThe code is as follows:

PS> Get-Something -Path test
You entered test.

PS> [Math]::Round

OverloadDefinitions                                                                                                      
-------------------                                                                                                      
static double Round(double a
static double Round(double value, int digits)
static double Round(double value, mode)
static double Round(double value, int digits, mode
static decimal Round(decimal d
static decimal Round(decimal d, int decimals)
static decimal Round(decimal d, mode)
static decimal Round(decimal d, int decimals, mode)