Powershell – Basics

It’s very interesting to learn and work with powershell. It’s very powerful and uses most of the Microsoft .Net Class Library. If we look at the basics

#Get-Command - Retrieves a list of all powershell commands
Get-Command

# Can expand by searching for just a verb or noun
Get-Command -verb "get"
Get-Command -noun "service"

# Get-Help can be used to explain a command
Get-Help Get-Command
Get-Help Get-Command -examples
Get-Help Get-Command -detailed
Get-Help Get-Command -full

#Most Command can also be passed a -? parameter to get help
Get-Command -?

#Moving around the file tree
#Get-ChildItem list all items in current path
Get-ChildItem

# set-Location will change the current path
Set-Location c:\app
Set-Location "C:\New_folder\"

# Pipelining -combine CmdLets for Power
Get-ChildItem | Where-Object {$_.Length -gt 1kb }

Get-ChildItem | Where-Object {$_.Length -gt 1kb } | Sort-Object Length

#Can break commands up among several lines
# (note pipe must be last char on line)
Get-ChildItem |
    Where-Object {$_.Length -gt 1kb } |
    Sort-Object Length
    
#To Specify columns in the output and get nice formatting, use Format-Table
Get-ChildItem |
    Where-Object {$_.Length -gt 1kb } |
    Sort-Object Length |
    Format-Table -Property Name, Length -AutoSize

#You can also use the Select-Object to retrieve certain properties from an object
Get-ChildItem |
    Select-Object Name, Length | Where-Object {$_.Length -gt 1kb }|
    Sort-Object Length
    
# .Net Everywhere
$str = "I am now a powershell novice"
$str            //Gets the string
$str.Length     //Gets the length of the string
$str.GetType()  //String

Leave a Reply

Your email address will not be published. Required fields are marked *