Thursday, August 17, 2017

Basic Powershell Scripting

Comments

Single line use: # Example
#This is a single comment in powershell
Multiple lines use <# #>
<#
This is a multiple lines comment.
It's used at the start of the script like the ones in the 80s
To put authors of script, version, contact information and help
#>

Variables

Native Variables: Just use the "$" symbol
$oneVar = "This is one var"
They are assigned in runtime depending of the values you provide.
$number2 = 2
If you wanna know what is the type of the variable you can use "A cmdlet" (commandlet), which is basically a "verb-word" phase, the most useful verbs are "get","set","disable" and the "word" are other items. The 1st cmdlet that I'm going to show you is the "Get-Member", PowerShell is an object oriented language, so, all it's threaded as an object.
This image shows "System.Int32" for the variable $a (similar to the previous definition of our "$number2 variable". This means that it's a .NET variable as well. Let's see what happend when we simulated the example of the "onvvar" variable.
It's a system.string also a .net object. This way to declare and initialize variables is called implicit. But we can also declare them explicitly, like this:
[int]$number2 = 2
[Datetime] $dt = Get-Date
In here we see our second cmdlet called "Get-Date" that retrieves the date of the system that you're currently working in. So in this 1st lesson let's end this little tutorial with the Hello Powershell
#Using a single line:
Write-Output "Hello Powershell's World"


#using several lines and defining variables
#implicitely
$hw = "Hello Powershell's World"
Write-Output $hw
write-Output "$hw"

#explicitely
[string]$hwe = "Hello Powershell's world"
write-Output $hwe
write-Output "$hw"
What is the difference between variable inside "" and outside them ?.

0 comments:

Post a Comment