PowerShell Common commands

Source: Internet
Author: User
Tags set set

PowerShell Common commands
1.get-command Get Powshell All orders
2.get-process Get all Processes
3.set-alias renaming the specified command such as: Set-alias AAA Get-command
4.set-executionpolicy remotesigned Setup PowerShell to directly execute script files Generic script files with the. PS1 end execution script file directly enter the file address to execute the script file only write commands
5.get-help get-* Query the command starting with Get Get-help *service* get-help Get-command get basic usage of Get-command command
6. Get-member Get object properties such as: $var | Get-memeber accesses the $var property directly $var. ToString ()


GET-HELP *: Lists all topics, including directives and concepts.
Get-help * | MORE: Lists all the topics, including instructions and concepts, and pauses when the full window is displayed.
Get-help about*: Lists all the conceptual topics, such as the million character, Foreach Loop.
Get-help get*: Lists all get-start topics.
Get-help {< instruction name or subject name;}: Lists instructions for the specified instruction or topic, such as Get-help dir can query the use of the dir instruction, and get-help About_wildcard can query the usage of the theme "Universal characters".
In addition, you can also use Help to replace the GET-HELP, the biggest advantage is that when the Help Display full window, the preset will automatically pause.

Definition of variables in PS
No need to define or declare data types
Add "$" before the variable
Rules for defining variables
-the variable can be a digital $123
-the variable can be a string $abc
-the variable can be a special character ${@1b}
Built-in variables
-$pshome
-$home
-$profile

Variable assignment: $var =123 $var = "AAAAAA"
Variable Value: $var

Variable assignment method: set-variable var 100
Value method: get-variable var
Empty value: clear-variable var
Delete variable remove-variable var
Take multiple variables such as var1 var2 var3 value: Get-variable var*

Another assignment method $var 1= "BBB" $var 2= "$var $var 1" result $var2= "aaaaaa BBB"
$var 2= ' $var $var 1 ' results $var2= "$var $var 1"
$date =get-date Get the current time
$date. AddDays (3) Current time plus 3 days

Sort usage
get-process | Sort-object ws from small to large sorted by WS value
get-process | Sort-object | FL get-process | Sort-object | Format-list displaying data in list form

Import Export File
Get-process > C:/aa.txt
get-process | Export-clixml c:/ddd.xml Exporting command execution results to an XML file
Import-clixml c:/ddd.xml Exporting an XML file to a control table


Note Use
get-proccess | #这里写注释信息
Sort ws

Comparison operators
$var = "ABC"
$var-like "&b&" returns True
$var-clike "&b&" returns True

function uses
Case: The following code is in a script file:
$var 1=10
function one{"The Variable is $var 1"}
function two{$var 1=20;one}
One
Both
One
Execution result: The Variable is 10
The Variable is 20
The Variable is 10
This example shows that changing the value of a variable in a function does not affect the actual value
If you need to change its value, see the following code:
$var 1=10
function one{"The Variable is $var 1"}
function two{$Script: Var1=20;one}
One
Both
One
Execution result: The Variable is 10
The Variable is 20
The Variable is 20

Freach use
$var =1..6 #定义数组
foreach ($i in $var)
{
$n + +
Write-host "$i"
}
Write-host "There were $n record"


If use
Get-service | foreach{
if ($_.status-eq "running") {
Write-host $_.displayname "(" $_status ")"-foregroundcolor "green"
}
Else
{
Write-host $_.displayname "(" $_status ")"-foregroundcolor "Red"
}
}

Error use
function one
{
Get-process-ea stop
Get-childitem ada-erroraction Stop #此句有误
Get-process-erroraction stop
}
One

-ea defines how to continue execution after an error occurs
$? You can test whether a command succeeds or fails, the result is true, and the reverse is false

Single-Step debugging
Set Set-psdebug-step First
for ($i =1; $i-le; $i + +)
{
Write-host "Loop number $i"
}

PowerShell Common commands

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.