Powershell stores information in objects, each object has a specific type, simple text is stored as type, and dates are stored as type. Any .NET object can return its type through the GetType() method, which has a FullName property in the type that can view the full name of the type.
PS C:Powershell> $date=get-date
PS C:Powershell> $date
January 11, 2012 15:19:49
PS C:Powershell> $().FullName
Each type can contain some static methods. You can get the type object itself through square brackets and type names, and then view all static methods supported by the type through the Get-Memeber command.
PS C:Powershell> [] | Get-Member -static -memberType *Method
TypeName:
Name MemberType Definition
---- ---------- ----------
Compare Method static int Compare( t1, ...
DaysInMonth Method static int DaysInMonth(int year, int month)
Equals Method static bool Equals( t1, ...
FromBinary Method static FromBinary(long dateData)
FromFileTime Method static FromFileTime(long fileTime)
FromFileTimeUtc Method static FromFileTimeUtc(long fileT...
FromOADate Method static FromOADate(double d)
IsLeapYear Method static bool IsLeapYear(int year)
Parse Method static Parse(string s), static Sy...
ParseExact Method static ParseExact(string s, strin...
ReferenceEquales Method static bool ReferenceEquals( objA, S...
SpecifyKind Method static SpecifyKind(...
TryParse Method static bool TryParse(string s, &, ...
TryParseExact Method static bool TryParseExact(string s, string format...
The static methods supported by the class are very practical
Use the Parse method to convert a string to the DateTime class:
PS C:Powershell> []::Parse("2012-10-13 23:42:55")
October 13, 2012 23:42:55
Use isLeapYear method to judge leap year
#Is 1988 a leap year?
[]::IsLeapYear(1988)
#Print all leap years from 1988 to 2000
for($year=1988;$year -le 2000;$year++)
{
if( []::IsLeapYear($year) ){$year}
}
True
1988
1992
1996
2000
Another commonly used class is the Math class, which defines many practical static methods in the Math class:
For example, find the absolute value, trigonometric function, round it:
PS C:Powershell> [Math]::Abs(-10.89)
10.89
PS C:Powershell> [Math]::Sin([Math]::PI/2)
1
PS C:Powershell> [Math]::Truncate(2012.7765)
2012
View the .NET type of interest
.NET supports thousands of types, and with these types you can do many things, and luckily Powershell happens to support them.
Object type conversion
For example, using a class to convert a string IP address into an IPAddress instance
PS C:Powershell> []'10.3.129.71'
Address : 1199637258
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IPAddressToString : 10.3.129.71
Calling static methods
The same class, check the host name according to the IP address, 8.8.8.8 is Google's free DNS server
PS C:Powershell> []::GetHostByAddress('8.8.8.8') | fl
HostName :
Aliases : {}
AddressList : {8.8.8.8}
Create instances based on type
The following demonstrates downloading files through the DownloadFile method of the $webClient class:
PS C:Powershell> $localName="C:"
PS C:Powershell> Test-Path $localName
False
PS C:Powershell> $add="/"
PS C:Powershell> $webClient=New-Object
PS C:Powershell> $($add,$localName)
PS C:Powershell> Test-Path $localName
True
View assembly
Type definitions in .NET are in different assembly settings. First of all, you must know which assembly has been loaded by the current program. The AppDomain class can accomplish this requirement because it has a static member CurrentDomain, and there is a GetAssemblies() method in CurrentDomain.
PS C:Powershell> [AppDomain]::CurrentDomain
FriendlyName : DefaultDomain
Id : 1
ApplicationDescription :
BaseDirectory : C:WINDOWSsystem32WindowsPowerShellv1.0
DynamicDirectory :
RelativeSearchPath :
SetupInformation :
ShadowCopyFiles : False
PS C:Powershell> [AppDomain]::()
GAC Version Location
--- ------- --------
True v2.0.50727 C:WindowsMicrosoft.NETFrameworkv2.0.50727mscorlib...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem2.0.0.0__b77a5c561...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Core3.5.0.0__b77a...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_32System.Transactions2.0.0.0...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Xml2.0.0.0__b77a5...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Management2.0.0.0...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.DirectoryServices...
True v2.0 C:WindowsassemblyGAC_MSILSystem....
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsMicrosoft.NETFrameworkv2.0.50727mscorlib...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_32System.Data2.0.0.0__b77a5c...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft....
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Configuration2.0....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.JScript8.0.0.0...
Search for the specified type
The method to query each assembly can be used using the GetExportedTypes() method. Because many assembly contains a large number of methods, it is best to specify keywords when searching. The following code demonstrates how to find types that contain the "environment" keyword.
PS C:Powershell> [AppDomain]::() | ForEach-Object { $_.GetExportedTypes() } | Where-Object { $_ -like $searchtext } | ForEach-Object { $_.FullName }
+SpecialFolder
There is a type searched above: class. Classes can do a lot of things. You can first check all the static methods of the following classes.
PS C:Powershell> [Environment] | Get-Member -Static
TypeName:
Name MemberType Definition
---- ---------- ----------
Equals Method static bool Equals( objA,
Exit Method static Exit(int exitCode)
ExpandEnvironmentVariables Method static string ExpandEnvironmentVariabl
FailFast Method static FailFast(string mes
GetCommandLineArgs Method static string[] GetCommandLineArgs()
GetEnvironmentVariable Method static string GetEnvironmentVariable(s
GetEnvironmentVariables Method static
GetFolderPath Method static string GetFolderPath(
GetLogicalDrives Method static string[] GetLogicalDrives()
ReferenceEquals Method static bool ReferenceEquals(
SetEnvironmentVariable Method static SetEnvironmentVaria
CommandLine Property static CommandLine {get;
CurrentDirectory Property static CurrentDirectory
ExitCode Property static System.Int32 ExitCode {get;set;
HasShutdownStarted Property static HasShutdownStart
MachineName Property static MachineName {get;
NewLine Property static NewLine {get;}
OSVersion Property static OSVersio
ProcessorCount Property static System.Int32 ProcessorCount {ge
StackTrace Property static StackTrace {get;}
SystemDirectory Property static SystemDirectory {
TickCount Property static System.Int32 TickCount {get;}
UserDomainName Property static UserDomainName {g
UserInteractive Property static UserInteractive
UserName Property static UserName {get;}
Version Property static Version {get;}
WorkingSet Property static System.Int64 WorkingSet {get;}
For example, the attributes in this article output the current login domain, username, and machinename:
PS C:Powershell> [Environment]::UserDomainName
MyHome
PS C:Powershell> [Environment]::UserName
xiaoming
PS C:Powershell> [Environment]::MachineName
LocalHost
Search Method
The following example shows how to search based on the specified keyword "Address".
[AppDomain]::() | ForEach-Object { $_.GetExportedTypes() } | ForEach-Object { $_.getmembers() } | Where-Object { $_.isStatic} | Where-Object { $_ -like $searchtext } | ForEach-Object { "[{0}]::{1} --> {2}" -f $_.declaringtype, $_.toString().SubString($_.toString().IndexOf(" ")+1), $_.ReturnType }
[]::Parse() -->
[]::IsLoopback() -->
[]::Any -->
[]::Loopback -->
[]::Broadcast -->
[]::None -->
[]::IPv6Any -->
[]::IPv6Loopback -->
[]::IPv6None -->
[]::Unknown -->
[]::Unspecified -->
[]::Unix -->
[]::InterNetwork -->
[]::ImpLink -->
[]::Pup -->
[]::Chaos -->
[]::NS -->
[]::Ipx -->
[]::Iso -->
[]::Osi -->
[]::Ecma -->
[]::DataKit -->
[]::Ccitt -->
[]::Sna -->
[]::DecNet -->
[]::DataLink -->
[]::Lat -->
[]::HyperChannel -->
[]::AppleTalk -->
[]::NetBios -->
[]::VoiceView -->
[]::FireFox -->
[]::Banyan -->
[]::Atm -->
[]::InterNetworkV6 -->
[]::Cluster -->
[]::Ieee12844 -->
[]::Irda -->
[]::NetworkDesigners -->
[]::Max -->
[]::GetBroadcastAddress -->
[]::AddressListQuery -->
[]::AddressListChange -->
[]::AddressListSort -->
[]::DestinationAddressRequired -->
[]::AddressFamilyNotSupported -->
[]::AddressAlreadyInUse -->
[]::AddressNotAvailable -->
[]::ReuseAddress -->
[]::ExclusiveAddressUse -->
[]::Invalid -->
[]::Tentative -->
[]::Duplicate -->
[]::Deprecated -->
[]::Preferred -->
[]::add_NetworkAddressChanged(System
.) -->
[]::remove_NetworkAddressChanged(Sys
) -->
[]::Parse() --> Syste
[]::None -->
[]::LinkLayerAddress -->
[]::PresentationA
ddress -->
[]::DoesNotHaveAnAddress -->
[]::WrongUseOfAddressOf -->