Type in PowerShell

简介: 1.Boolean A "true" or "false" value. This type doesn't include any significant methods or properties.

1.Boolean

A "true" or "false" value. This type doesn't include any significant methods or properties. Note that PowerShell includes built-in variables named $true and $false that represent the two possible Boolean values:

PS C:/> [boolean]$b = $true
PS C:/> $b
True
PS C:/>

2.DateTime

A date, time, or date-and-time value. Note that all DateTime values include both a date and a time component. If you don't specify a date or a time, then that portion of the value is typically set to zero when the value is stored. Methods and properties include:

AddDays

AddHours

AddMilliseconds

AddMinutes

AddMonths

AddSeconds

AddTicks

AddYears

Adds the specified interval to the value. To subtract a value, specify a negative interval. Note that the type does include a Subtract() method; from within a PowerShell script one of these "Add" methods is generally easier to work with.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.adddays(1)

Sunday, January 02, 2000 3:00:00 PM

GetDateTimeFormats

Returns all possible string representations of the value.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.getdatetimeformats()
1/1/2000
1/1/00
01/01/00
01/01/2000
00/01/01
2000-01-01
01-Jan-00
Saturday, January 01, 2000
January 01, 2000
Saturday, 01 January, 2000
01 January, 2000
Saturday, January 01, 2000 3:00 PM
Saturday, January 01, 2000 03:00 PM
Saturday, January 01, 2000 15:00
Saturday, January 01, 2000 15:00
January 01, 2000 3:00 PM
January 01, 2000 03:00 PM

(output truncated in this example)

IsDaylightSavingTime

Indicates whether this instance of DateTime is within the Daylight Saving Time range for the current time zone.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.isdaylightsavingtime()
False

ToFileTime ToFileTimeUTC

Converts the value to a Windows file time. The "UTC" version provides a Universal Time Constant version of the value.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.tofiletimeutc()
125912124000000000
PS C:/> $d.tofiletime()
125912412000000000

ToLocalTime

Converts the value to a local time.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.tolocaltime()

Saturday, January 01, 2000 7:00:00 AM

ToLongDateString

ToLongTimeString

ToShortDateString

ToShortTimeString

Converts the value to the appropriate date or time format.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.tolongdatestring()
Saturday, January 01, 2000
PS C:/> $d.toshortdatestring()
1/1/2000

ToUniversalTime

Converts the value to a Universal Time Constant (UTC), taking time zone changes into account.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.touniversaltime()

Saturday, January 01, 2000 11:00:00 PM

3.Double

A double-precision floating point number, capable of including fractions. Can store values ranging from negative 1.79769313486232e308 to positive 1.79769313486232e308. Methods and properties include:

ToString

Returns a string representation of the value.

PS C:/> [double]$d = 123.456789
PS C:/> $d.tostring()
123.456789

Day

DayOfWeek

DayOfYear

Hour

Millisecond

Minute

Month

Second

Ticks

TimeOfDay

Year

Returns only the specified portion of the value.

PS C:/> [datetime]$d = "1/1/2000 3:00 PM"
PS C:/> $d.day
1
PS C:/> $d.month
1
PS C:/> $d.dayofweek
Saturday
PS C:/> $d.timeofday


Days              : 0
Hours             : 15
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 540000000000
TotalDays         : 0.625
TotalHours        : 15
TotalMinutes      : 900
TotalSeconds      : 54000
TotalMilliseconds : 54000000

4.Int

An integer (non-fractional) value. Can store values ranging from negative 2,147,483,648 through positive 2,147,483,647. Methods and properties include:

ToString

Returns a string representation of the value.

PS C:/> [double]$d = 123.456789
PS C:/> $d.tostring()
123.456789

5.String

A string of text characters. Methods and properties include:

Clone

Returns a reference to this instance of String.

PS C:/> [string]$a = "hello"
PS C:/> $a
hello
PS C:/> $b = $a.clone()
PS C:/> $b
hello
PS C:/> $a
hello
PS C:/> $b = "world"
PS C:/> $b
world
PS C:/> $a
hello
PS C:/>

Contains

Returns a value indicating whether the specified String object occurs within this string.

PS> [string]$a = "hello"
PS> $a.contains("e")
True

EndsWith

Determines whether the end of an instance of String matches a specified string.

PS C:/> [string]$a = "hello"
PS C:/> $a.endswith("lo")
True

Equals

Determines whether two String objects have the same value.

PS C:/> [string]$a = "hello"
PS C:/> [string]$b = "world"
PS C:/> $a.equals($b)
False

IndexOf

Reports the index of the first occurrence of a String or one or more characters within this string.

PS C:/> [string]$a = "hello"
PS C:/> $a.indexof("l")
2

Insert

Inserts a specified instance of String at a specified index position in this instance.

PS C:/> [string]$a = "hello"
PS C:/> $b = $a.insert(2,"-st-")
PS C:/> $b
he-st-llo

LastIndexOf

Reports the index position of the last occurrence of a specified Unicode character or String within this instance.

PS C:/> [string]$a = "hello"
PS C:/> $a.lastindexof("l")
3

Length

Gets the number of characters in this instance.

PS C:/> [string]$a = "Hello"
PS C:/> $a.length
5

PadRight

Left-aligns the characters in this string, padding the right with spaces or a specified Unicode character for a specified total length.

PS C:/> [string]$a = "hello"
PS C:/> "-" + $a.padright(8) + "-"
-hello   -

Remove

Deletes a specified number of characters from this instance.

PS C:/> [string]$a = "hello"
PS C:/> $a.remove(3,2)
hel

Replace

Replaces all occurrences of a specified Unicode character or String in this instance with another specified Unicode character or String.

PS C:/> [string]$a = "hello"
PS C:/> $a.replace("l","p")
heppo

Split

Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char or String array.

PS C:/> [string]$a = "one,two,three"
PS C:/> $b = $a.split(",")
PS C:/> $b[0]
one
PS C:/> $b[1]
two
PS C:/> $b[2]
three

StartsWith

Determines whether the beginning of an instance of String matches a specified string.

PS C:/> [string]$a = "hello"
PS C:/> $a.startswith("f")
False

Substring

Retrieves a substring from this instance.

PS C:/> [string]$a = "hello"
PS C:/> $a.substring(2,3)
llo

ToCharArray

Copies the characters in this instance to a Unicode character array.

PS C:/> [string]$a = "hello"
PS C:/> $b = $a.tochararray()
PS C:/> $b[0]
h
PS C:/> $b[4]
o

ToLower

Returns a copy of this String converted to lowercase.

PS C:/> [string]$a = "Hello"
PS C:/> $a.tolower()
hello

ToUpper

Returns a copy of this String converted to uppercase.

PS C:/> [string]$a = "Hello"
PS C:/> $a.toupper()
HELLO

Trim

Removes all occurrences of a set of specified characters from the beginning and end of this instance.

PS C:/> [string]$a = "  hello  "
PS C:/> "-" + $a.trim() + "-"
-hello-

TrimEnd

Removes all occurrences of a set of characters specified in an array from the end of this instance.

PS C:/> [string]$a = "  hello  "
PS C:/> "-" + $a.trimend() + "-"
-  hello-

TrimStart

Removes all occurrences of a set of characters specified in an array from the beginning of this instance.

PS C:/> [string]$a = "  hello  "
PS C:/> "-" + $a.trimstart() + "-"
-hello -

7.Advanced Types

PowerShell also includes support for a number of advanced types that we don't cover. These include:

  • XML (System.Xml.XmlDocument)

  • Scriptblock (System.Management.Automation.Scriptblock)

These are very advanced classes, particularly in the case of the XML type, that require a good bit of instruction to utilize.

Because manipulating these types within a PowerShell script is outside the scope of what most Windows administrators do,

we've elected not to include these types. However, given the .NET Framework class names (shown in parentheses),

you can easily research these at http://msdn.microsoft.com/library if you find you have need of them.

8.All Types

In case you're interested in the complete list of available types, here it is:

  • [int] - Integer

  • [int[]] - Integer array

  • [long] - Long integer

  • [long[]] - Long integer array

  • [string] - String of characters

  • [string[]] - String array

  • [char] - Single character

  • [char[]] - Array of characters

  • [bool] - True or False; [Boolean] is also valid

  • [bool[]] - Array of Boolean values

  • [byte] - Byte value

  • [double] - Double-precision floating number

  • [decimal] - Decimal number

  • [float] - Floating number

  • [single] - Single-precision floating number

  • [regex] - Regular expression; the .NET Framework class is System.Text.RegularExpressions.Regex

  • [array] - array

  • [xml] - XML document; the .NET Framework class is System.Xml.XmlDocument

  • [scriptblock] - a script block; the .NET Framework class is System.Management.Automation.ScriptBlock

  • [hashtable] - a hashtable or associative array; the .NET Framework class is System.Collections.Hashtable

  • [ref] - a PowerShell reference; the .NET Framework class is System.Management.Automation.PSReference

  • [psobject] - a PowerShell object; the .NET Framework class is System.Management.Automation.PSObject

  • [wmi] - a WMI object; the .NET Framework class is System.Management.ManagementObject

  • [wmisearcher] - a WMI Searcher object; the .NET Framework class is System.Management.ManagementObjectSearcher

  • [wmiclass] - a WMI class; the .NET Framework class is System.Management.ManagementClass

目录
相关文章
|
2月前
|
XML 运维 监控
PowerShell实战:Get-Content命令使用详解
【2月更文挑战第12篇】 Get-Content 主要作用是获取路径指定位置的项(文本类文件)的内容,例如文件中的文本或函数的内容。 对于文件,内容一次读取一行,并返回对象的集合,每个对象表示一行内容。
|
搜索推荐 Shell
Win10小技巧:将在此处打开命令改为CMD或Powershell
微软的 CMD 也就是命令提示符已经伴随了我们很多年了,这些年来 CMD 已经勤勤恳恳地默默工作着,虽然没有图形化的界面,但是其干净整洁的操作环境仍然受到了大家的欢迎。
432 0
Win10小技巧:将在此处打开命令改为CMD或Powershell
|
Go
Go get报错:terminal prompts disabled
Go get报错:terminal prompts disabled
423 0
Go get报错:terminal prompts disabled