PowerShell Basic Knowledge
PowerShell: Based on .Net Framework platform
1995-2007 BAT / VBS script
2008 Windows PowerShell V1.0 (Windows Server 2008)
2009 Windows PowerShell V2.0 (Windows Server 2008 R2)
2012 Windows PowerShell V3.0 (Windows Server 2012)
2013 Windows PowerShell V4.0 (Windows Server 2012 R2)
V2.0 Add remote management, Add module, 260 commands
V3.0 Add command (more than 1000 commands), Add module dynamic load, Add tab function, Add workflow, Add powershell web access (PSWA)
V4.0 Add command (more than 1200 commands), Add Desired State Configuration (DSC)
Example
Get-service: display all the windows service
Get-service -name alg: display the windows service alg
Cmdlet: Get-service
Parameter: -name
Actual Parameters: alg
Stop-service -name alg: stop alg service
Start-service -name alg: start alg service
Get-disk: display the disk information
Get-physicaldisk: display the physical disk information
Get-command: display all the command
Get-vm: display all the virtual machine(Hyper-V)
New-vm: create the virtual machine(Hyper-V)
Remove-vm -name <vm name>: remove the virtual machine(Hyper-V)
Cmdlet format: verb + noun
verb: get-verb
Get / Stop / Start / Add / Connect / Disconnect / Install / Uninstall / Read / Enable / Disable / Format /.....
ps1 file: powershell script file
Get-alias: display all the alias
Customer define alias
new-alias -name <alias name> -value <alias value>
Example: new-alias -name testalias -value get-disk
Execution Policy
Check: Get-executionpolicy
Change: Set-executionpolicy -Executionpolicy {unrestricted | remotesigned | allsigned | restricted | default | bypass | undefined}
Example: set remote execution
set-executionpolicy remotesigned
Pipe
Example: get-service bits | stop-service
Example:
1. new-vm -name 01
new-vm -name 02
new-vm -name 03
2. get-vm -name "01","02","03" | remove-vm
PowerShell Tools
1. Windows PowerShell ISE (Integrated Scripting Environment)
2. PowerShell GUI
3. Visual Studio
Help
Get-help get-service -examples: get the help of the command with examples
Get-help get-service -full: get the help of the command with the description of the parameters
Update-help: upgrade help informatino
save-help -destination <path>: save help
Filter
Example: get-windowsfeature | where {$_.name -like "*web*"} | where {$_.name -like "*net*"}
$_: means the objects which transfer by the piple
-like: operation character
Other operation characters: -match(string) / -eq(number) / -Cmatch(attention for the upper or lower) / -le / -lt
Properties
(get-windowsfeature)[1] | select-object *: display all the properties of the object
(get-windowsfeature)[1] | select-object name,featuretype,displayname: only display specific properties
(get-windowsfeature)[1] | select-object name,featuretype,displayname | ft -autosize: display with proper format
get-windowsfeature | where {$_.featuretype -match "role"}: display all windows feature which the featuretype is role
(get-windowsfeature)[1].name
Install & Uninstall Role / Features
Get-WindowsFeature: check the install status of windows features
Get-WindowsFeature | where {$_.name -like "*core*"}: key words filter
Add-WindowsFeature <Feature name, Feature name, ...>
Example: add-windowsfeature net-framework-core
Example: install features with key words "web", "net"
get-windowsfeature | where {$_.name -like "*web*"} | where {$_.name -like "*net*"} | add-windowsfeature whatif: not really install check the process
get-windowsfeature | where {$_.name -like "*web*"} | where {$_.name -like "*net*"} | add-windowsfeature
Variable
declare: $var
$var="1"
display: write-host $var
$var
check properties and methods: $var | get-member
$service=get-service bits
$service.stop()
$service.start()
Filesystem: get-psdrive
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
Alias Alias
C 48.41 174.66 FileSystem C:\ Users\Tom
cert Certificate \
D 114.22 108.84 FileSystem D:\
E FileSystem E:\
Env Environment
F FileSystem F:\
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
J FileSystem J:\
Variable Variable
WSMan WSMan
Y 466.40 1350.11 FileSystem Y:\
Z 466.40 1350.11 FileSystem Z:\
List all the environment variables: dir env:
Check the register HKEY_LOCAL_MACHINE\system: cd HKLM: --> cd system
map driver: map folder with new name
Example: map "c:\demo" as demo
new-psdrive -name demo -psprovider filesystem -root c:\demo
go to demo: cd demo:
Find command related to event:
help *event*
help get-eventlog
Find command related to reboot:
help *reboot*
Find detail description and examle: help dir -full
If the parameter of the command is string[], it means command can be followed by several parameters.
Example: dir c:\, c:\windows, 'c:\program files'
Run two level command:
computer.txt
localhost
127.0.0.1
get-service -Computername (get-content .\computers.txt)
Find command deal with process: get-command -noun *proc*
Find command deal with event: get-command -noun *event*
Find command deal with get: get-command -verb *get*
Find command related with service: get-command *service* -commandtype cmdlet
Find command related with out: get-command -verb out
out command:
dir | out-file dir.txt -append == dir >> dir.txt
export the result to csv file: get-service | export-csv c:\services.csv
export the result to xml file: get-service | export-clixml c:\services.xml
export the result to html file: get-service | convertto-html | out-file services.html
Use compare to find difference
1. get-process|export-clixml c:\baseline.xml
2. add some applications: calc, notepad, mspaint
3. compare-object -referenceobject (import-clixml c:\baseline.xml) -differenceobject (get-process) -property name
Snapin
Check registered snap-in: get-pssnapin -registered
Module
Check module: get-module -listavailable
Check Windows Service status. If not running, start it.
$ServiceName="AdobeARMservice"
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
}
Create as a windows schedule task
powershell -file "<powershell file name>"
Example
program: powershell
add arguments: -file "C:\AutomatedScripts\Check_FileZilla.ps1"
Comments
Post a Comment