PowerShell 第一天

 1.    Overview

To my understanding, PowerShell is more or less of a commandline shell which integrated with .Net framework. You can utilize a lot of objectsdefined in .Net Framework. You can access everything in COM or WMI, so that youcan control the services on local/remote machine.

PowerShell can execute 4 types of things:

  1. Regular command line commands, such as “cd”, “dir”,“copy”, in fact, those commands were just aliases to some .Net cmdlets, not theoriginal commands.
  2. Cmdlets: .Net programs designed to interact withPowerShell
  3. PowerShell scripts (.ps1 file)
  4. PowerShell functions
  5. Standalone executable

When execute something without relative/full path specified,PowerShell will look for the commands in this order:

  1. Alias
  2. Function
  3. Cmdlet
  4. Application
  5. Script
  6. files

To use PowerShell, you need to start PowerShell window oruse powershell.exe. When PowerShell window is started, by default, it loadssome libraries; you can import more libraries by edit PowerShell window profile.

When a PowerShell window is started, a windows script hostis created, $hostreference to the current host.

Output of execution will be one single value or array of variantvalues. The output can be the input of another executable by using “|” topipeline the commands.

Useful tip:

  1. Use get-help or helpor command -? wheneveris needed.
  2. PowerShell window support auto-finish-typing,use tab will automatically finish the word, more tab will switch among a listof satisfied choices.
  3. Use ` to continue with next line for command cross multi lines

Getgenuine PowerShell commands:  Get-Command -commandType cmdlet

Use“&” to change astring to a command

  1. Use PowerShell window as calculator
  2. Decimal numbers: (23+7)/3.5 (Enter) , you will get 8.57142857142857
  3. Number with unit: 4GB/720MB(Enter) , you willget 5.68888888888889
  4. Convert 16-bit to decimal: “0xaf” will give you175.
  5. Trustworthy Subdirectories
  6. env: is currentenvironment
  7. Alias: has all the aliasdefined
  8. Function: has all the functions
  9. List all members in the subdirectory: Dir [name]: i.e. dir env: will list all the variables defined

2.    Keyboard combination

Useful short-keycombinations:

Key Meaning
(Alt)+(F7) Deletes the current command history
(PgUp), (PgDn) Display the first (PgUp) or last (PgDn) command you used in current session
(Enter) Send the entered lines to PowerShell for execution
(End) Moves the editing cursor to the end of the command line
(Del) Deletes the character to the right of the insertion point
(Esc) Deletes current command line
(F2) Moves in current command line to the next character corresponding to specified character
(F4) Deletes all characters to the right of the insertion point up to specified character
(F7) Displays last entered commands in a dialog box
(F8) Displays commands from command history beginning with the character that you already entered in the command line
(F9) Opens a dialog box in which you can enter the number of a command from your command history to return the command. (F7) displays numbers of commands in command history
(Left arrow), (Right arrow) Move one character to the left or right respectively
(Arrow up), (Arrow down),(F5), (F8) Repeat the last previously entered command
(Home) Moves editing cursor to beginning of command line
(Backspace) Deletes character to the left of the insertion point
(Ctrl)+(C) Cancels command execution
(Ctrl)+(End) Deletes all characters from current position to end of command line
(Ctrl)+(Arrow left), (Ctrl)+(Arrow right) Move insertion point one word to the left or right respectively
(Ctrl)+(Home) Deletes all characters of current position up to beginning of command line
(Tab) Automatically completes current entry, if possible

3.    Cmdlet

Cmdlets arespecialized commands in the PowerShell environment that implement specificfunctions. Cmdlets follow a <verb><noun>namingpattern, such as Get-ChildItem, helping to make them self-descriptive.

A list of cmdletscould be saved in one ps1 file and execute them as a batch.

Make sure youhave enough privilege to execute all the cmdlets. Or you can change the host’sexecution policy by calling Set-ExecutionPolicy.

Most useful cmdlet:  Get-Help,   i.e.  “get-helpget-*” will give you the list of cmdlets started with “get-“.

4.    Parameter type:

  1. Fixed positioned
  2. Named
  3. Switch, turn on/off something, such as –Debug,-Verbose
  4. Value, a value must be provided, such as –WorkingDirectory[path]

5.    Aliases

You can give commands other names.

  • Define an alias: Set-Alias [alias] [command]
  • Resolve an alias: $alias:[aliasname]
  • remove an alias:

o  by default, alias will be removed when you quit PowerShellwindow

o  you can save the definition to an ps1 file and reloadit by calling “

o  Del alias:[alias]

  • List all available aliases: Dir alias:
  • Reusealias definition by save the definition to a ps1 file

o  Import-Alias [filename], with option [-Force] will force the alias to bereplaced if already exists.