1th. PowerShell Interactive Interface

Source: Internet
Author: User

1.1 Running programs, scripts, and existing tools:

Program.exe arguments

SCRIPTNAME.PS1 arguments

Batchfile.cmd arguments

If you run a command name that contains spaces, enclose the command in quotation marks and precede the symbol &, which is called the Invoke operation, for example:

& ' C:\Program files\program\program.exe ' arguments

Run the file in the current directory

. \program.exe arguments

Run commands in the current directory that contain spaces in the command name, plus symbols & and. \, for example:

& '. \program Files\program\program.exe ' arguments

At the PowerShell prompt, perform an expression operation:

    

1.2 Running PowerShell commands

The Get-process command is a built-in PowerShell command, called a cmdlet, that provides important functionality for administrators and developers.

1> uses the same command syntax

2> supports rich pipeline capabilities

3> output easy-to-manage objects rather than error-prone text

  

1.3 Automatic with Shell, profile and prompt

PowerShell defaults to storing the address of your script file in the $profile variable, which allows you to access the script file.

Create a new script file (overwrite the existing):

New-item-type File-force $profile

To edit a file that already exists:

Notepad $profile

To view the contents of a configuration script file:

Get-childitem $profile

  

To modify the caption of the control window:

$host. Ui. Rawui.windowtitle = "title"

To modify the color of the output hint:

Write-host-foregroundcolor darkgray "Change the color of the output hint"

  

1.4 Finding commands to implement a specified task

Gets the summary information for the specified command:

Get-command CommandName

Gets the details of the specified command, redirecting the output of Get-command to the format-list command:

Get-command CommandName | Format-list

To search for all commands that contain "text", use an asterisk *, for example:

Get-command *text*

To search for all commands that use the GET verb, enter the parameter get after-verb, as follows:

Get-command-verb Get

To search all service-related commands, add the parameter service after-noun, as follows:

Get-command-noun Service

1.5 getting command Help

Get a summary help for a command, mainly including the outline of the command, syntax, and details of the description:

Get-help CommandName

Or CommandName-?

Get the details of a command's help information, including parameter descriptions and examples:

Get-help commandname-detailed

Get detailed help information for a command that contains all the parameter descriptions and comment information:

Get-help Commandname-full

Get an example of a command, as follows:

Get-help Commandname-examples

For more information about GET-HELP:

Get-help Get-help

1.7 Calling the PowerShell script outside of PowerShell

To invoke a PowerShell script from a batch file, logon script, scheduled task, or other non-PowerShell application:

PowerShell "&" full path to script ' arguments '

Example: PowerShell "& ' C:\shared scripts\get-report.ps1 ' Hello world '

1.9 Getting the system date and time

Get-date

Check today is the day of the week, as follows:

$date =get-date

$date. DayOfWeek

    

Get-help Get-date can see more commands about Get-date

1.10 Checking the status of the last Run command

PowerShell provides two variables to monitor the success of the last executed command, these two variables are $lastexitcode and $?

Variable $lastexitcode:

A numeric type that returns the exit code or error level returned by the last script or application execution

The variable is $?:

Boolean that returns the success of the last execution of the command true or failed false

  

1.11 Calculating the time of the command execution

Calculate the time of a command execution:

Measure-command {start-sleep-milliseconds 337}

    

1.12 PowerShell shortcut keys

Up forward Search History command

Down Backward Search History command

PgUp show the first command in a history command

Pgdown show the last command in the history command

Left moves the cursor one character

Right moves the cursor one character

Home on the command line, move the cursor to the start position

End on the command line, move the cursor to the end position

Ctrl+left move the cursor one word to the left

Ctrl+right move the cursor one word to the right

1.14 Usage and Management console history

To get the most recently used command:

Get-history

To get the specified command from the history command, you can pass the command's ID to invoke-history, as follows:

Invoke-history Id

By assigning a value to $maximumhistorycount, you can increase or limit the number of commands stored in the session history

$MaximumHistoryCount =count

To save the history command to a file, you can use the pipeline command:

Get-history | Export-clixml Filename

Load the saved commands into the console and use the pipeline redirection to

Import-clixml Filename | Add-history

1.15 Saving the output of the command to a file

Save the results of the command output to a file by using the Out-file command or the redirect operator

Out-file

Get-childitem | Out-file UnicodeFile.txt

Get-content Filename.cs | Out-file-encoding ASCII File.txt

Get-childitem | Out-file-width UnicodeFile.cs

REDIRECT operator

Get-childitem > File.txt

Get-childitem 2> file.txt

1.16 adding information to the end of a file

Directs pipeline output to a file, and appends information to the file

Use the parameters of the Out-file command-append

Get-childitem | Out-file-append Files.txt

REDIRECT operator

Get-childitem >> Files.txt

1.17 Record your session transcript

Logs or full text of the current session

Start-transcript Path

Stop-transcript

Path is optional and is used to specify the file name of the record, based on the current system time. By default, this file is saved to the My Documents directory and Stop-transcript if you want to stop logging.

1.18 Display the properties of an item as a list

To display the details of an item, you can use the pipe command function to output an item to the Format-list command, for example:

$currentError = $error [0]

$currentError | Format-list-force

There are three formatting commands in PowerShell: Format-table, Format-wide, and format-list,format-list get input to be displayed in a list.

By default, PowerShell obtains the properties to display from the *.format.ps1xml file in the installation directory. If you want to display all the properties, you can type Format-list *.

There are times when you type Format-list *, but you cannot get a list of properties for an item, which occurs because the item is defined in the *.format.ps1xml file, but there are no entries defined in the List command to display the Chinese medicine.

In this case, you can type Format-list-force

1.19 Display the properties of an item as a table

The output of the command is piped to format-table, as follows:

get-process | Format-table

    

To display specific properties

get-process | Format-table Name,ws

    

Notifies PowerShell to format the table in the most readable manner, providing the-auto parameter to Format-table

get-process | Format-table Name,ws-auto

    

Custom columns (displays the working set of a process in megabytes), which can provide a custom format

$fields = "Name", @{label= "WS (MB)"; Expression={$_. WS/1MB}; Align= "Right"}

get-process | Format-table $fields-auto
    

1.20 error output for pipeline commands

Lists all errors that occur in the current session list and accesses the $error array:

$error

Lists the last error in the session list, that is, accessing the first value in the $error array

$error [0]

List the details of the error, pipe output to format-list, note to add-force

$currentError = $error [0]

$currentError | Format-list-force

Lists the details of the command that caused the error, you can access the Invocationinfo property

$currentError = $error [0]

$currentError. Invocationinfo

Display errors in a more concise taxonomy-based manner, modifying the value of the variable $errorview

$errorView = "Categoryview"

Clear PowerShell-generated errors

$error. Clear ()

1.21 Configuring debug, checksum, and processing output

The output of the debug information for the startup script and the command that generated it

$debugPreference = "Contiune"

Start-debugcommand

Enable check mode for commands

Copy-item c:\temp\*.txt C:\temp\backup\-verbose

Disable progress information generated by a script or command

$progressPreference = "Silentlycontinue"

Ger-process.ps1

Many scripts and commands also produce several other outputs, including:

Debug output: Diagnosing problems

Write-debug

or call WriteDebug ()

Output this type of information. except through $host. privatedata.debug* Color Configuration variables to define the color of the output information, otherwise the information will be displayed in yellow

Validation output: Monitoring the actions of a command

Write-verbose

or call Writeverbose ()

Output this type of information except through $host. privatedata.verbose* Color Configuration variables to define the color of the output information, otherwise the information will be displayed in yellow

Progress output: Monitoring the status of long-running commands

Write-process

or call Writeprogress ()

Output this type of information except through $host. privatedata.progress* Color Configuration variables to define the color of the output information, otherwise the information will be displayed in yellow

Some commands only produce inflammation or debug information if you specify the-verbose or-debug parameters separately. To configure debugging, validation, or progress information for a script or command, you can modify the shell variables by modifying the

$debugPreference, $verbosePreference, and $progresspreference to achieve the goal, the acceptable values for these variables are:
Quiet mode (silentlycontinue): Output information is not displayed

Stop mode (STOP): treats output as an error

Continuation mode (CONTINUE): Display output

Inquiry Mode (Inquire): Display continue operation information

1th. PowerShell Interactive Interface

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.