PowerShell Utility Commands (1)

Source: Internet
Author: User
Tags floor function

PowerShell has become a skill that Windows Ops people have to master, his strength is only used to realize, gossip not to say, here to record the PowerShell of the utility commands, help in the PowerShell struggle between the children's shoes

Turn the result of the output into the type of object you want, for example, I have a decimal, but I just want his integer digits, here we can use the-as operator, the type that you want to convert after as is [int],[string], etc.

1.23 -as [int]

This command is to convert 1.23 decimal to integer value, so finally we get the result is 1, this function is often used for some disk space or data conversion, it should be noted that this conversion using the rounding mechanism, that is, if the decimal value is greater than or equal to 0.5 will be in the integer digits plus 1
Of course we don't use the as operator, and the simpler way is to directly precede the value or variable with a type such as [int]1.23, so the output value is also a

There are several ways to take the whole, and here are some examples, sometimes we may need to calculate the decimal place directly into an integer, or the decimal place directly, then we can encounter this situation through [Math]::floor () and [Math]::ceiling () to execute, for example:

PS C:\Windows\system32> $a=2.33PS C:\Windows\system32> [math]::Ceiling($a)3PS C:\Windows\system32> [math]::Floor($a)2

From the above code, we can see that the ceiling function is rounded up, that is, the decimal place to all the integers on the 1,floor function is to take down the whole, the decimal place is ignored, do not care you are more than 0.5

and the previously mentioned [int] directly to the integer, there is also a way, is [Convert]::toint32 ($a), can also get rounded rounding results, of course Toint32 function is not just a simple conversion of decimals, he can also be used to convert 2 binary, 10 binary, 16 binary and so on, such as

PS C:\Windows\system32> $a = [convert]::ToString(188,2)PS C:\Windows\system32> [convert]::ToInt32($a)10111100PS C:\Windows\system32> [convert]::ToInt32($a,2)188PS C:\Windows\system32> [convert]::ToInt32($a,8)2134592PS C:\Windows\system32> [convert]::ToInt32($a,16)269553920PS C:\Windows\system32> [convert]::ToInt32($a,10)10111100

First I assign the 188 to 2 in the form of a variable (listen to the form, it is actually a string value, to confirm that you can directly use $a.gettype () to see his type), and then test directly into the integer, then turn 2, 8, 16 and 10 into the system, well, looks nothing wrong, That's right, old iron.

Okay, let's move on to the next more practical command, using a hash expression to convert the input format
I would like to write a script when you export data, you should encounter the output of the field name of the results you feel dissatisfied, want to change to the name you want, do not care why, just want to change, there is a sentence how to say, I just like you see I am not accustomed to and take my no way, is so wayward.
OK, small case, hash expression to help you, for a simple example, I would like to change the input field English to the Chinese I want to see the code:

PS C:\Windows\system32> Get-Process | select name -First 3Name                ----                AcroRd32            AcroRd32            ApplicationFrameHost

We use get-process catch 3 process name, you can find the Name field is name, as a patriotic five good youth, I have to change this name to Chinese, see My Code DAFA

PS C:\Windows\system32> Get-Process | select @{name=‘名称‘;expression={$_.Name}} -First 3名称                  --                  AcroRd32            AcroRd32            ApplicationFrameHost

See no, name no, my Chinese kanji instead, name and expression can be abbreviated to N and e @{n= ' desired name '; e={can be either a value of code output or a value}}

To extend, we know that usually we use FT or fl such output format command when the output value is not specified in the Alignment function, PowerShell will default to align, but this will find that there are some left-aligned, some right-aligned, but also can play well, not to the whole 2 alignment of the way to do, I only need one alignment, we can solve by the hash expression, only need to add align= ' left ' or ' right ' in the expression, we can let the data to align to the other side, or to align it, for example @{n= ' name '; e={$_.name};align= ' ' Left ' }

Today is the point, want to know more, and listen to tell, adjourned!

PowerShell Utility Commands (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.