Powershell Common commands
1.Get-Command
obtain Powshell All commands
2.Get-Process Get all processes
3.Set-Alias Rename... To the specified command Such as :Set-Alias aaa
Get-Command
4.Set-ExecutionPolicy remotesigned Set up powershell The script file can be executed directly
In general, script files are written as .ps1 ending Execute the script file and enter the file address directly Just write the command in the script file
5.Get-Help get-* Query to get The first order
Get-Help *service* Get-Help Get-Command obtain Get-Command Basic usage of commands
6. Get-Member
Get object properties Such as : $var | Get-Memeber visit $var attribute direct $var.ToString()

get-help *: List all the topics , Including instructions and concepts .
get-help * |
more: List all the topics , Including instructions and concepts , And when the display is full, the whole window pauses .
get-help about*: List all the conceptual topics , For example, 10000 yuan characters 、foreach
loop .
get-help get*: List all get The subject at the beginning . ?
get-help
{< Instruction name or subject name >}: Lists a description of the specified instruction or topic , for example get-help dir You can query dir Instruction usage , and get-help
about_wildcard You can query 「 Universal characters 」 The usage of this topic .
Besides , It can also be used. help replace get-help, The biggest benefit is that help
Show the whole window , The default will automatically pause .

PS Variable definition in
There is no need to define or declare data types
Add... Before the variable "$"
Rules for defining variables
- Variables can be numbers
$123
- Variables can be strings $abc
- Variables can be special characters ${@1b}
Built in variables
-$pshome
-$home
-$profile

Variable assignment : $var=123 $var="aaaaaa"
Take the value of the variable : $var

Variable assignment method :Set-Variable var 100
Method of value taking : Get-Variable var
Empty value : Clear-Variable
var
Delete variables Remove-Variable var
Take multiple variables such as var1 var2 var3 Land value : Get-Variable var*

Another method of assignment $var1="bbb" $var2="$var $var1" result $var2="aaaaaa bbb"
$var2='$var
$var1' result $var2="$var $var1"
$date=Get-Date Get the current time
$date.AddDays(3)
Current time plus 3 God

Sort usage
Get-Process | Sort-Object
ws according to WS Rank values from small to large
Get-Process | Sort-Object | fl Get-Process | Sort-Object |
Format-List Display data as a list

Import and export files
Get-Process > c:/aa.txt
Get-Process | Export-Clixml
c:/ddd.xml Export command execution results to xml In file
Import-Clixml c:/ddd.xml take xml Export the file to the console

Annotation use
Get-Proccess |
# Here's the comment information
sort ws

Comparison operator
$var="abc"
$var -like "&b&" return true
$var -clike
"&b&" return true

Function USES
Case study : There is the following code in a script file :
$var1=10
function
one{"The Variable is $var1"}
function two{$var1=20;one}
one

two
one
Execution results : 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, please see the following code :
$var1=10
function
one{"The Variable is $var1"}
function two{$Script:var1=20;one}
one

two
one
Execution results : The Variable is 10
The Variable is 20
The Variable
is 20

freach Use
$var=1..6
# Define an array
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
# This sentence is wrong
Get-Process -ErrorAction stop
}
one

-ea Define how to continue execution when an error occurs
$? You can test whether a command succeeds or fails , Success leads to true Instead of false

Step by step debugging
First set up Set-PSDebug
-step
for($i=1;$i -le 10;$i++)
{
Write-Host "loop number
$i"
}

Powershell More articles about common commands

  1. PowerShell Common commands and beautification ( Modern CMD)

    PowerShell It's a modern terminal , It's Microsoft that replaces the old CMD Of . PowerShell With object-oriented thinking , Very convenient . Common commands Download the file ( Here to install.ps1 File as an example ) $client = n ...

  2. bash and powershell A collection of common commands

    Linux Shell # 1. Running commands in the background nohup python xxx.py & # Search and replace ## Only all in the catalog .py and .dart Recursive search character in file "main ...

  3. cmd/powershell Common commands git Common commands

    cmd/powershell: 1. New folder : mkdir directoryName 2. New file : cmd: type nul>fileName ( Empty file ) powershell: n ...

  4. PowerShell Common commands

    Download the file http://powershell.com/cs/blogs/tips/archive/2012/10/11/downloading-files-from-internet.aspx Pow ...

  5. powershell Common command to get disk partition information

    // see mac Address PS C:\Users\yyy> get-wmiobject -class Win32_NetworkAdapterConfiguration -namespace &qu ...

  6. Windows PowerShell Basic grammar and common commands

    PowerShell Common commands : One Get class 1.Get-Command : Get all the PowerShell command , Get about cmdlet And about Windows PowerShell Other elements of the command ...

  7. PowerShell frequently-used .Net 、COM object (New-Object、Assembly)、 Load assembly

    # New random number object instance :$Ran = New-Object System.Random$Ran.NextDouble() occasionally , The class of the instance to be used is stored in a separate library file ,PowerShell Not loaded by default , Meeting ...

  8. Windows Run common commands (win+R)

    Windows Run common commands (win+R) 1.calc: Start the calculator  2.notepad: Open Notepad  3.write: Writing board  4.mspaint: Drawing board  5.snippingtool: Screenshot tool , ...

  9. Windows Common command summary of Intranet penetration

    Common commands for in domain information collection net group /domain // Get a list of all domain user groups net group zzh /domain // In the display field zzh A member of the group net group zzh /del /do ...

Random recommendation

  1. Java Program log :java.util.logging.Logger class

    One .Logger The level of Than log4j The level of detail , All defined in java.util.logging.Level Inside . The levels are listed in descending order as follows :SEVERE( Maximum value )WARNINGINFOCONFIGFINEF ...

  2. JMeter Study ( thirty-five ) Use jmeter To send the json/gzip Format data

    One . Use jmeter To send the gzip data Sometimes we need to simulate after the client compresses the data , send out (post) To the server side . This is usually the case , It will happen on mobile terminals . The benefits of doing this , It can save traffic .   Of course , The server returns ...

  3. Talking about Struts2( Four )

    One .Struts2 Interceptor (Intercept) effect : The multiple Action Common code in , Extract to interceptor , Thereby reducing Action Redundant code in . 1.Action Interceptor a. To write interceptor class pu ...

  4. Linux/UNIX Data files and information systems

    Data files and information systems Password file In the storage /etc/passwd stay . The following functions can be used to obtain password file entries . #include <sys/types.h> #include <pwd.h> ...

  5. EF framework ~Migration The execution order of data migration

    Back to directory For a single branch project , As long as you generate one migration Version of , There will be a corresponding timestamp file , And in the update-database when , It will start from the smallest time , All the way to the current version of migration, and ...

  6. python Regular spaces \xa0 Real record And xpath take div All the text with multiple labels inside

    When I play reptiles in my spare time , From the original original way of writing Change it to scrapy The framework , Use native selector when ,xpath When using regularization to retrieve and read numbers , Small problems ,mark Next . First get The data blocks I need ,( I use sc ...

  7. Ubuntu TensorFlow Source code Android Demo Compile run of

    Ubuntu TensorFlow Source code Android Demo Compile run of One . install Android Of SDK and NDK SDK To configure A: download Choose the latest domestic download address : SDK: https://d ...

  8. c# Summary of interview questions (1)

    c# Summary of interview questions From: https://www.cnblogs.com/suzhiyong1988/p/5069385.html   The following reference answers are just to help you understand , Don't carry it , Interview questions . The written test questions are ever-changing ...

  9. C The ternary operator of language

    grammar : expression 1 ? expression 2 : expression 3; Equivalent to if( expression 1) { expression 2 } else { expression 3 }

  10. tomcat Set up the error page

    Today's study notes : When we visit tomcat A non-existent page of , The return error message is as follows : Such an interface is not user-friendly , Sometimes it's not safe , Therefore, you need to modify the default error page . vim /$TOMCAT_HOME/co ...