Last active
June 12, 2019 10:33
-
-
Save jindeveloper/1e14e401a84783a9c71765a16022ef95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# start of block comment | |
Why powershell? | |
* it object oriented | |
* rich script environment | |
* bulk operations | |
* interactive shell | |
* task automation | |
* use of consistent, repetable task | |
* work with built in provider | |
end of block comment#> | |
#single line comment | |
#variables in powershell starts with a dollar sign ($) | |
#variables only exists inside the current powershell window | |
$var = 2 | |
Write-Host $var | |
$var2 = "Hello Powershell"; | |
Write-Host $var2 | |
<# | |
As you can see powershell automatically figures out what are the data types of your variables. | |
However; if you can you can explicitly say to powershell what the datatype is see an example below: | |
#> | |
[string]$var3 = "Say What? $var2" | |
Write-Host $var3 | |
#What are the data types we can use in Powershell: integers, floating points values, strings, booleans and datetime values. | |
#region data-types | |
$sum = 1+1 #integer | |
Write-Host $sum | |
Write-Host $sum.GetType() #get the value type | |
$stringConcat = "Hello Again" + $sum + "x" | |
Write-Host $stringConcat | |
Write-Host $stringConcat.GetType() | |
<# | |
Notes to remember: | |
About $true and $false it is built in variables in Powershell used for displaying true and falses. | |
#> | |
$youLovePowershell = $true; | |
$youLoveMsDOS = $false; | |
Write-Host $youLovePowershell | |
Write-Host $youLovePowershell.GetType() | |
#Write-Host -is $true -eq 1 | |
#Write-Host -is $false -eq 0 | |
$currentDate = Get-Date | |
Write-Host $currentDate | |
Write-Host $currentDate.GetType() | |
#endregion | |
<# | |
Powershell Comparison Operators | |
-eq (equal) | |
-ne (not equal) | |
-lt (less than) | |
-le (less than or equal) | |
-gt (greater than) | |
-ge (greater than or equal) | |
#> | |
1 -eq 1 #equal | |
1 -ne 1 #not equal | |
1 -lt 1 #less than | |
1 -le 1 #less than or equal | |
1 -gt 1 #greater than | |
1 -ge 1 #greater than or equal | |
<#powershell arrays#> | |
$fruits = @('apple', 'banana', 'orange','avocado') | |
Write-Host $fruits[0] | |
foreach($item in $fruits){ | |
Write-Host $item | |
} | |
$myFruits = @('apple', 'avocado', 'banana', 'mango','dragon-fruit') | |
foreach($fruit in $myFruits) | |
{ | |
if($fruit -eq 'apple') | |
{ | |
Write-Host -ForegroundColor "Green" $fruit | |
continue | |
} | |
Write-Host $fruit | |
} | |
function sayHello(){ | |
return "Hello function" | |
} | |
$result = sayHello | |
Write-Host $result | |
#region Strings | |
$name = "Powershell" | |
Write-Host $name.ToLower() | |
Write-Host $name.ToUpper() | |
Write-Host $name.Contains("shell") | |
$newName = $name.Replace("shell", "shells") | |
Write-Host $newName | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment