Commands
Helpful Information
If you type the beginning of a command and press tab, options for that command will print out in the terminal. For example you can type
Get-and all the options forGetwill be printed out.PowerShell scripts will end in
.ps1
Write-Host
Used within scripts to create output for the user and display data being processed.
Get-Date
Outputs the current date in the terminal.
Get-Command
Will output all available CmdLets.
Get-Command -Verb Get
Output all Get commands. We use the -Verb
Get-Command -Noun Host
outputs all commands acting on host in the terminal
Get-Content
Used to access file content, reads the file line by line
Select-String
Used to return lines with specific content
Clear-Host
Use this to clear the terminal session
Get-History
Access the terminal command history
Get-Help
Displays help about Powershell cmdlets and concepts
Get-Host
Output information about your host
Get-Timezone
Outputs your timezone
-Prompt
Outputs a string and then waits for the user to press enter.
Read-Host
Allows you to prompt the user for input.
Variables and Operators
Data comes in various forms:
Numbers (integers): 1,-99,42
Text (strings): "Hello", "BF1991"
Booleans (True/False)
How Variables Work
In computer programming, variables are used to store a piece of data. In PowerShell variables can store the results of commands and expressions like values, names, paths, and settings.
To create a variable, we must assign it a value. Variables in PowerShell are referenced using a dollar sign ($) followed by a variable name. After a variable reference, we use an equal sign followed by the value we are assigning to it.
$my_string_variable = "Hello, World"Variable names:
Not case-sensitive
Can include spaces and special characters when enclosed in curly brackets.
If you assign a variable and want to know its variable you can use the following command:
Write-Host $my_input
To figure out what data type a variable is we can add the following to the end of the variable:
$my_input = "Hello World!"$my_input.GetType().NameThe output will then be "string"
Constrained Variables
If we want to enforce a certain type on a variable, we can create a constrained variable via casting.
Create Multiple Variables
Environmental Variables
Environmental variables store information related to the current environment, like the OS and user sessions in the terminal. They are global variables, which means we can access them across commands and programs. They are usually created by the operating system, but we can use them to configure our production environment.
Environment Variables are stored as strings
To get all existing environmental variables, we can use the following:
Get-ChildItem Env:
Use the following:
The
PATHvariable is a popular environment variable that includes all directories where applications look for executables.The
HOMEvariable is another popular environmental variable that specifies the current user's home directory.The following is the syntax to create an environmental variable:
The variable names are usually fully capitalized
Arithmetic Operators
Operators are used to perform specific operations on data often stored in variables. PowerShell has multiple types of operators:
Operators are best used with variables!
Arithmetic Operators
Add
+Subtract
-Multiply
*Divide
/Modulus
%
Assignment Operators
The Following are compound assignment operators:
= is the default assignment operator that we know
Unary Operators
++ increases the value of a variable by 1
-- decreases the value of a variable by 1
Comparison Operators
Comparison operators are used to compare values, test conditions, or filter elements of a collection such as an array. They return a boolean operators (true or false)
-eq
Checks if the two operand values are an exact match
-ne
Checks if the two operand values are NOT an exact match
-gt
Checks if the value of the left operand is greater than the value of the right operand
-lt
Checks if the value of the left operand is less than the value of the right operand
-ge
Checks if the value of the left operand is greater OR equal to the value of the right operand
-le
Checks if the value of the left operand is less OR equal to the value of the right operand
Logical Operators
Logical operators allow us to combine multiple True/False expressions and statements into complex conditionals.
Truth Table
T
T
T
T
F
F
T
F
F
T
T
F
F
T
F
T
T
T
F
F
F
F
F
T
Last updated