5.1.2 Tools & code analysis: Analyzing Scripts in Powershell Flashcards
Coding in PowerShell: how comment are written in Powershell (2)?
▪ # This is the first line of my script
▪ <# This is a comment block. You can use this to comment out large sections of text or code in your scripts. #>
Coding in PowerShell: what is a variable in Powershell and how to spot it ?
A variable is a placeholder for a value that can change. It is used to store data for use in a script or a command.
In PowerShell, you can create a variable by using the dollar sign ($) followed by the variable name, for example: $myVariable = “Hello, World!”
Coding in PowerShell: what is an array in Powershell and how to spot it ?
An array is a data structure that can hold multiple values. You can think of it as a collection of items that are stored together under a single variable name. To spot an array in PowerShell, you can look for the following characteristics:
1/ Variable Name with @ Symbol
2/ Comma-Separated Values Inside Parentheses
Exemple: $myArray = @(1, 2, 3, 4, 5)
$myArray is the variable and @(1, 2, 3, 4, 5) is the array
Coding in PowerShell: what is a named array in Powershell and how to spot it ?
A named array is a collection of key-value pairs, where each value is associated with a unique key or name. This allows you to access the values using their specific names rather than numeric indices.
To spot a named array in PowerShell, you can look for the following characteristics:
1/ Variable Name with @{} Syntax. Exemple:
$myNamedArray = @{
“Name” = “John”;
“Age” = 30;
“City” = “New York”;
}
Coding in PowerShell: what is a Associative array in Powershell and how to spot it ?
An associative array is another term for a “named array” or a “hash table”. It is a collection of key-value pairs, where each value is associated with a unique key or name. This allows you to access the values using their specific names rather than numeric indices. To spot an associative array (hash table) in PowerShell, you can look for the following characteristics:
1/ Variable Name with @{} Syntax:
$myAssociativeArray = @{
“Name” = “John”;
“Age” = 30;
“City” = “New York”;
}
Coding in PowerShell: provide the basic writing for arithmetic in Powershell (=, !=,<,> etc)
$a and $b are the variable.
▪ is equal to: $a -eq $b
▪ is not equal to: $a -ne $b
▪ is greater than: $a -gt $b
▪ is greater than or equal to: $a -ge $b
▪ is less than: $a -lt $b
▪ is less than or equal to: $a -le $b
Coding in PowerShell: how to write conditional statement in PowerShell?
In PowerShell, you can write conditional statements using the if, elseif, and else keywords. Here’s an example of how to write a simple conditional statement. Example:
$number = 10
if ($number -gt 5) {
Write-Host “The number is greater than 5”
} elseif ($number -eq 5) {
Write-Host “The number is equal to 5”
} else {
Write-Host “The number is less than 5”
}
Coding in PowerShell: list the loops in Powershell (3)
1/ For
2/ Do While
3/ Do Until
Coding in PowerShell: explain the ‘for’ loops in Powershell
1/ For Loop: The for loop is used to iterate a specific number of times. Here’s an example:
for ($i = 1; $i -le 5; $i++) {
Write-Host “The value of i is $i”
}
In this example, the loop initializes $i to 1, checks if $i is less than or equal to 5, and then increments $i by 1 in each iteration.
Coding in PowerShell: explain the ‘Do While’ loops in Powershell
The do while loop is used to execute a block of code repeatedly as long as a specified condition is true. Here’s an example:
$counter = 1
do {
Write-Host “The value of counter is $counter”
$counter++
} while ($counter -le 5)
In this example, the loop continues to execute as long as the condition $counter -le 5 (counter is less than or equal to 5) is true.
Coding in PowerShell: explain the ‘Do Until’ loops in Powershell
Do Until Loop: The do until loop is used to execute a block of code repeatedly until a specified condition is true. Here’s an example:
$counter = 1
do {
Write-Host “The value of counter is $counter”
$counter++
} until ($counter -gt 5)
In this example, the loop continues to execute until the condition $counter -gt 5 (counter is greater than 5) becomes true.
Coding in PowerShell: how to Input Data in powershell (2)
1/ Read-Host: Allows you to prompt the user for input and store the input as a string. Example:
$name = Read-Host “Enter your name”
2/ $args Variable: Allows you to access command-line arguments passed to a script or function. Example:
param($name)
Write-Host “Hello, $name”
Coding in PowerShell: how to Output Data in powershell (5)
1/ Write-Host: Outputs the specified string to the console. Example:
Write-Host “Hello, World!”
2/ Write-Output: Sends the specified object down the pipeline. Example:
$result = Get-Process
Write-Output $result
3/ Pipeline (|): Allows you to pass the output of one command as input to another command. Example:
Get-Service | Where-Object {$_.Status -eq “Running”}
4/ Out-File: Sends output to a file rather than the console. Example:
Get-Process | Out-File -FilePath “processes.txt”
5/ Export-Csv: Sends output to a comma-separated values (CSV) file. Example:
$data | Export-Csv -Path “data.csv” -NoTypeInformation
Coding in PowerShell: how to Read Data into Files in powershell (3)?
1/ Get-Content: Retrieves the content of a file and outputs it as a collection of strings. Example:
$content = Get-Content -Path “file.txt”
2/ Import-Csv: Reads the content of a CSV file and converts it into objects that can be manipulated in PowerShell. Example:
$data = Import-Csv -Path “data.csv”
3/[System.IO.File]::ReadAllText: Uses the .NET framework to read the contents of a file into a string. Example:
$content = [System.IO.File]::ReadAllText(“file.txt”)
Coding in PowerShell: how to Write Data into Files in powershell (4)?
1/ Set-Content: Writes the specified content to a file. If the file does not exist, it is created. Example:
$content | Set-Content -Path “output.txt”
2/ Out-File: Sends output to a file, overwriting the existing content or creating a new file if it doesn’t exist. Example:
“Hello, World!” | Out-File -FilePath “output.txt”
3/ Export-Csv: Writes objects to a CSV file. Example:
$data | Export-Csv -Path “output.csv” -NoTypeInformation
4/[System.IO.File]::WriteAllText: Uses the .NET framework to write a string to a file, overwriting the existing content. Example:
[System.IO.File]::WriteAllText(“output.txt”, “Hello, World!”)