Sunday, March 10, 2019

Powershell - Renaming multiple files/folders at once

Get-ChildItem -Path C:\projects *document-manager-* -Directory | ForEach-Object -Process {Rename-item -Path $_.Name -NewName ($_.name -replace "document-manager-","") -whatif}


Get-ChildItem -Path C:\projects *document-manager-* -Directory | ForEach-Object -Process {Rename-item -Path $_.Name -NewName ($_.name -replace "document-manager-","") -Verbose}

Friday, March 1, 2019

Powershell - Basics

Powershell is a command line interface

Based on .NET and everything in a .NET object

introduced in 2003 under code name Monad.

Version 1  release in 2006 as optional add-on

Veresion 2 was included in Windows 7 and windows server 2008 R2, XP SP3, Vista SP1.

Commands in PS are called Command-lets
Command-lets have verb-noun syntax like Get-Help, Get-Command, Set-Location... etc

Basic commands:

1. Get-Command
2. Get-Command -verb "get"
3. Get-Command -noun "service"
4. Get-Help Get-Command
5. Get-Command -?
6. Set-Location


Pipelining : in powershell, combining the commands is called pipelining
Ex:
Get-ChildItem | Where-Object { $_.Length -gt 10000kb }

This command finds all files which has length greater than 10000KB in size.

Extending the example :

Get-ChildItem | Where-Object { $_.Length -gt 100000kb }
Sort-Object Length
Select-Object Name, Length
Format-Table -Property Name, Length -Autosize


Clear-Host : Clears the screen


Variables in PS:

 All variables start with $

$myvariable = "Hello Variable"
$myvariable

This prints "Hello Variable" on console window. Everything is a .NET object including variables.
This is a string type variable and in next line we are printing the variable value.

Type can be see by $myvariable.GetType()

So all methods which are associated with string data type in .NET are applicable. For example :
$myvariable.ToUpper() returns HELLO VARIABLE.


Similarily, integer operations can be carried out on integer variables. For example

$age = 32
$age -gt 30

This returns True as 32 is greater than 30. Just demoing here that integer operation has been used here
Conversions:

"42" -eq 42

What will it show?
It will be True. Because string 42 is compared with numeric 42 and hence integer 42 is first converted to string and then compared. 
Rule is that the right side operand is converted to datatype of left side operand.


Alternate syntax of declaring a variable:
New-Variable -Name var -Value 123

equivalent to :
$var = 123


same way
Get-Variable var is equivalent to $var

Other variable related commands:
Set-Variable -Name var -Value 900
Clear-Variable -Name var
Remove-Variable -Name var
Get-Variable
Get-Variable var


Escape sequence:
Backtick N:  `n
Backtick B: `b
Backtick t: `t

@ sign:

$var = @'
Some text here

   a bit more

 a blank line above
'@

Using expressions in string

"There are $((Get-ChildItem).Count) items"

"The 15% tip of 33.33 dollar bill is $(33.33*0.15) amount"

[string]::Format("There are {0} items. " , $((Get-ChildItem).Count) )

PS shortcut for this is:

"There are {0} items" -f $((Get-ChildItem).Count)

Wildcards:

"Mysite" -like "My*"   # Returns True  (asterick)
"Mysite" -like "?ysite" # Returns True (single character)
"Mysite" -like "My*[e-t]" # Return True as matching last character in the range of characters ( range check)

Regex:
"888-368-1240" -match "[0-9]{3}-[0-9]{3}-[0-9]{3}"


Arrays:
$array = "Robert", "Cain"
$array

$array = @("Robert", "Cain") 

Creating an empty array:
$array = @()
$array.Count

$array = 1..5 
$array # prints values 1 to 5

$array -contains 42 # checks whether array contains value 42

$array -notcontains 42

Hashtables

$hash = @{ "Key" = "Value"; "Site" = "mysite.com"; "user" = "vikas" }
$hash
$hash["Site"] # prints mysite.com

$mykey = "user"
$hash.$mykey #prints vikas 

$hash["newkey"] ="newvalue"  # a new key is added



$hash.remove("newkey")
$hash.Contains("Site") 
$hash.CintainsValue("vikas") # Prints True by finding value vikas

$hash.Keys  #Prints all keys
$hash.Values #Prints all values

Automatic Variables:
PS has some auto defined variables:

1. $pwd : current directory
2. $host : info about user machine
3. $PID : process id
4. $PSVersionTable : versions for PS
5. $_ : Current object
6. $Home : user's home directory
7. $NULL
8. $true and $false



Branching :

1. if else 

if($var -eq 1)
{
    "if condition"
}
else 
{
    "else condition"
}

Nesting:


if($var -eq 1)
{
    "if condition 1"
}
else 
{
  if($var -eq 2)
  {
    "if condition 2"
  }
  else 
  {
    "else condition 2"
   }
    "else condition 1"
}



Switch statement:

switch($var)
{
   41 { "forty one" ; break}
  42 { "forty two" break}
  default { "default" }
}

switch(3,1,2,42)
{
   1 {"one"}
   2 { "two"}
   3 {"three" }
  default {"default"}
}

By default switch is case insensitive. To make it case sensitive , we need to write
switch -casesensitive ("mysite")

Switch also supports wildcards:

switch -wildcard ("mysite")
{
    "mysi*": {"matched"}
}



Looping:

While Loop:

$i = 1
while ($i -le 5)
{
    "`$i = $i"
     
}



Do while Loop:
$i = 1
do
{
   "`$i = $i"
   $i++
} while ($i -le 5)


Do Until Loop

$i = 1
do
{
   "`$i = $i"
   $i++
} until($i -gt 5)

For loop:
for($i = 0; $i -lt 5; $i++)
{
    $i
}


For-Each Loop:

$array = 11, 12, 13, 14, 15

foreach($item in $array)
{
    $item
}

break and continue can be used in Foreach loop


using Labels:


Script Blocks

$cool = { Clear-Host; " I am cool block" }
& $cool


Sending parameters to scriptblocks:

two ways 
# Using parameters:
$qa = {
     $question = $args[0]
    $answer = $args[1]

    write-host " Question is $question and answer is $answer"
}

&$qa "my question" "my answer"


#Using param command  (recommended as it is cleaner approach) : 

$qa = {
   param ($question,  $answer)
    write-host " Question is $question and answer is $answer"
}

&$qa  -question "my question"  -answer "my answer"
&$qa  -q"my question"  -a "my answer"  # shortcuts for param names
&$qa  "my question"  "my answer"

if you don't pass the param :

$qa = {
   param ($question,  $answer = "default empty answer ")   # default value if answer param is not passed.
    write-host " Question is $question and answer is $answer"
}

&$qa  -question "my question"  



Error handling :
New keyword Trap is just like catch block in C#

function Get-Div($n1, $n2)
{
     return $n1 / $n2
   trap
   {
      "error occurred"
      write-host $_.Exception
   }
   
}

Clear-Host
Get-Div 10 0


Get-Content: is used to get the contents of a file.
For ex:

$a = Get-Content "C:\\a.txt"

Set-Content: used to set the contents in a file.

Export-Csv used to convert collection into a CSV file. 

Import-Csv: used to get the contents of csv and set into a collection.


Out-File: to send output to a file.


Import-Module

Import-Module "C:\intro\MainFunctions.ps1"


Comments:

Single line comments : #
Multi line comments:  <# #>


Opening a File :

Invoke-Item

Invoke-Item "C:\a.txt"