5.1.2 Tools & code analysis: Analyzing Scripts in Powershell Flashcards

1
Q

Coding in PowerShell: how comment are written in Powershell (2)?

A

▪ # 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. #>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Coding in PowerShell: what is a variable in Powershell and how to spot it ?

A

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!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Coding in PowerShell: what is an array in Powershell and how to spot it ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Coding in PowerShell: what is a named array in Powershell and how to spot it ?

A

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”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Coding in PowerShell: what is a Associative array in Powershell and how to spot it ?

A

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”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Coding in PowerShell: provide the basic writing for arithmetic in Powershell (=, !=,<,> etc)

A

$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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Coding in PowerShell: how to write conditional statement in PowerShell?

A

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”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Coding in PowerShell: list the loops in Powershell (3)

A

1/ For
2/ Do While
3/ Do Until

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Coding in PowerShell: explain the ‘for’ loops in Powershell

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Coding in PowerShell: explain the ‘Do While’ loops in Powershell

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Coding in PowerShell: explain the ‘Do Until’ loops in Powershell

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Coding in PowerShell: how to Input Data in powershell (2)

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Coding in PowerShell: how to Output Data in powershell (5)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Coding in PowerShell: how to Read Data into Files in powershell (3)?

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Coding in PowerShell: how to Write Data into Files in powershell (4)?

A

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!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly