0

Introduction to PowerShell Basics


<#
.SNOPSYS
Excute Basic Powershell commands for absoulute beginners

.DESCRIPTION
This script is meant for education/demonstration purpose only
and not meant for production use

- This whole script is self explanatory
- The aim of this script is is introduce few basic of powershell which
would help to kick start using powershell

.NOTES
Author: Susheel Dakoju
Date Authored: 04/14/2015
#>
#--------------------------------------------------------------------------------
# Example 1: Windows PowerShell cmdlets similar to the .NET framework. See below
# Execute the following and see for yourself
# Most of the commandlet are in the form of Verb-Noun fashion
# 'Let' as told by Dictionary.com a diminutive suffix attached to nouns (booklet, piglet, ringlet, anklet, wristlet).
#--------------------------------------------------------------------------------
Get-Date
(Get-Date).GetType()
#--------------------------------------------------------------------------------
#Examplet 2: Similar to how you would do in .NET. ToShortDateString()can change the output format.
#The idea behind is example is show how simliar the experience would be for a .NET developer
#--------------------------------------------------------------------------------
(Get-Date).ToShortDateString().GetType()

#------------------------!IMPORTANT-----------------------------------------------
# Example 3: PIPELINES
# This is one of the most important concepts where one command can create object and pass on to the next command let
# This is not new and has been part for scripting for years.
# Another imprtant thing to notice is the Format-table option which lets you format your output.
#--------------------------------------------------------------------------------
Get-Process | Sort-Object -Property CPU -Descending | Select-Object -First 4 -Property CPU,VirtualMemorySize | Format-Table
#------------------------!IMPORTANT-----------------------------------------------
# Example 4: Alias
# Powershell ships with is own aliases this is for a good reason.
# For instance cls will work, but at the back ground it execure Clear-Host cmdlet
# You can create your Alias; Remembers Aliases are valid only per Session they
# get purged once session close use Import-Alias and Export-Alias commandlets
#--------------------------------------------------------------------------------
Get-Alias -Name dir # = Get-ChildItem
New-Alias ListItMyWay get-childitem

#--------------------------------------------------------------------------------
# Example 5: Variables and DataTypes
# Execute the following and check for yourself
#--------------------------------------------------------------------------------
$StoreMyInteger = 4
$StoreMyInteger.GetType()

$StoreMyString = "PowerShell Rocks!"
$StoreMyString.GetType()

Comparison and logical operators

#--------------------------------------------------------------------------------
# Example 6: Variables and DataTypes
# Execute the following and check for yourself
<#
#--------------------------------------------
a. Comparision Operators
#--------------------------------------------
-eq Equal
-ne Not equal
-ge Greater than or equal
-gt Greater than
-lt Less than
-le Less than or equal
-like Wildcard comparison
-notlike Wildcard comparison
-match Regular expression comparison
-notmatch Regular expression comparison
-replace Replace operator
-contains Containment operator
-notcontains Containment operator
-ceq Case-sensitive Equals
-creplace Case-sensitive replace
#--------------------------------------------
b. Types
#--------------------------------------------
-is Is of a type
-isnot Is not of a type
-as As a type, no error if conversion fails

#--------------------------------------------
c. Logical operators
#--------------------------------------------
-and Logical And
-or Logical Or
-not logical not
! logical not

#--------------------------------------------
d. Bitwise operators
#--------------------------------------------
-band Bitwise and
-bor Bitwise or

#--------------------------------------------
e. Format Operator
#--------------------------------------------
"format_string" -f Object1, Object2,...
#>
#>
#-----------
# Working with 'is' Operator: Re
#-----------
$stringValue = "PowerShell"
$stringValue -is "String"
# Just for fun know the retun Type
$($stringValue -is "String").GetType()

#-----------
# Working with 'eq' and 'ceq': Equal and Case-Sensitive Equal Operator
#-----------
$smallCase = "abc"
$UpperCase = "aBc"
$smallCase -ceq $UpperCase #return False
$smallCase -ceq $UpperCase #return True

#-----------
# Working with 'Like'
#-----------
"susheel" -like "sus*" #return true

#-----------
# Working with 'Replace'
#-----------
"Windows Command Prompt" -replace "Command Prompt","Power Shell"

#end of Example 6: Variables and DataTypes

#--------------------------------------------------------------------------------
# Example 7: Arrays
# These are similar to .NET Array. See ways to declare and use them
# The interesting part is to assign items of different types similar to C# Object Type
#--------------------------------------------------------------------------------
$array1 = @('1','2', (Get-Alias -Name cls))
#--------------------------------------------------------------------------------
# Example 8: Script Flow and Execution Control
# Only gave example for if, else. Try the others by yourself
# Conditional Execution: 1. if elseif else | 2. Switch | 3. Switch -option
# Iteration Statements: 1. For | 2. For Each | 3. While | 4. do while
#--------------------------------------------------------------------------------

if ($status) {
$output = "time Service is enabled"
} Else {
$output = "time service disabled"
}
Write-Output $output

#--------------------------------------------------------------------------------
# Example 9: Functions
# A very basic function to add numbers and how to execute it.
#--------------------------------------------------------------------------------

Function AddNumbers([int] $value1, [int] $value2)
{
return ($value1 + $value2)
}

AddNumbers 1 2

#----------------------***! Very Important !***----------------------------------
# Example 10: Execution Policy
# A very basic function to add numbers and how to execute it.
<#
a. Restricted: No script execution is allowed.

B. RemoteSigned: Script execution is allowed only for the scripts authored
on the local computer but not for any scripts from the external sources

Not be executed unless they are signed by a trusted publisher.

C. AllSigned: All the scripts must be signed by a trusted publisher including
scripts authored on the local computer

D. Unrestriced: All scripts ARE executed without any restriction.
#>
#--------------------------------------------------------------------------------
#----------------------
# Fetch the current Execution Policy
#----------------------
Get-ExecutionPolicy

#----------------------
# Set Execution Policy
#----------------------
Set-ExecutionPolicy -ExecutionPolicy Restricted

&nbsp;