Archive for April 1, 2013

Powershell Crash course part – 1

April 1, 2013 Leave a comment
Windows PowerShell exists?

Windows PowerShell exists? (Photo credit: crazytales562)

Hi All,

in this efforts , I will cover some PS basics to get myself and you started on Powershell..

Powershell helps DBA’s to achieve some complex functionality which are not possible by t-sql and command prompt

PS 1 had around 130 commands ,PS2 and PS3 have far more commands now

InstallingPS ..

PS wont get install on windows 2000 or older version then that ..

Newer OS like windows 7 and higher have Powershell preinstalled in that ..

for windows XP,windows 2003 etc go to to get your setup (check whether you need 32 or 64 bit)

to check whether PS is already available for your OS .. either enter powershell.exe in the START–> run prompt

OR go to add/remove program winmdows componets and add Powershell component ..

customizing the SHELL ..

you can right click on the edge of the prompt to customize the size of the window to suite ur preference …

getting Started :: (ur script may not run for that you have to set your execution policy to > set – executionpolicy unrestricted (refer this link for details PowerShell Basics – Execution Policy and Code Signing Part 1 ( ))

all the commands (also called as command-lets or cmdlets) in PS are in the form of VERB-noun form

even if the command returns multiple results the noun is singular ..

for example type get-process on the PS prompt (this will list all the process currently running on system )

PS D:\powershell\myscript> get-process

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
——- —— —– —– —– —— — ———–
194 4 3632 652 46 1.67 4140 AcroRd32
349 9 55876 29360 162 109.86 4700 AcroRd32
277 8 5296 712 61 5.59 3452 AdobeARM

below is my default prompt .. that i have set my powrshell to start in ..

PS D:\powershell\myscript> 

you will most probably start in in PS C:\document and setting\yourID >

to read the content of a file example text file use  :: get-content

PS D:powershell\myscript> get-content myfile.txt

all commandlets take parameters to acheive different funalities and parameters are positional … meaning you may skip typing parameter name if you passing the actual value of parameter

PS D:powershell\myscript> get-content myfile.txt

is same as PS D:powershell\myscript> get-content -path myfile.txt

where –path was the parameter

in PS if a articular path has a space in it use double quotes for it

PS D:powershell\myscript> get-content “C:\test files\myfile.txt”

ALIASES :: to make all administrators (*nix,cmd.exe) switch to PS an easier curve .. PS has all *nix working in PS too .. so you already many PS commands without knowing it 🙂

example :: cd ,dir,mkdir,del,ren,remdir will all work in PS without any issues ..

as all of them are aliases to PS under hood commands

PS D:powershell\myscript> get-alias  CD
CommandType Name Definition
———– —- ———-
Alias cd Set-Location

so CD is alias of PS command named SET-LOCATION

to get a list of all alias just type –> get-alias

creating a new alias (it will be available only in your session : to make it persistent you need to save it in ur profile )

PS D:powershell\myscript> new-alias N notepad

PS D:powershell\myscript> N 

above command will open up new notepad window .. you can create aliases of ur preferences ..

Thanks for reading .. will continue in part -2