Explore tens of thousands of sets crafted by our community.
Basic PowerShell Scripting
37
Flashcards
0/37
Events
PowerShell can respond to system events using Register-ObjectEvent. Syntax: `Register-ObjectEvent -InputObject
Variables
Variables store data that can be used and manipulated throughout a PowerShell script. Syntax: `
HashTables
HashTables are a collection of key-value pairs and are created using @{}. Syntax: `
Functions
Functions are blocks of code designed to perform a particular task and can be reused. Syntax: `function Verb-Noun { ... }`
Modules
Modules are packages of related PowerShell cmdlets, functions, variables, and more. Syntax to import a module: `Import-Module ModuleName`
Sorting Objects
Cmdlet Sort-Object sorts objects by property values. Syntax: `Get-Process | Sort-Object -Property CPU`
Providers
Providers allow access to data stores, such as the file system, registry, or environment variables. Syntax: `Get-Item -Path Env:Path`
Data Types
PowerShell supports various data types like integers, strings, booleans, and arrays. Syntax for array: `
Operators: Arithmetic
Arithmetic operators include +, -, *, /, % for calculations. Syntax: `first +
Conditional Statements
Conditional statements control the flow of execution based on conditions. Syntax: `if(condition) { ... } else { ... }`
Loops: For
For loops execute a block of code a set number of times. Syntax: `for (i -lt 10; i++) { Write-Host i }`
Loops: While
While loops continue executing as long as a condition is true. Syntax: `while (
PowerShell Profiles
Profiles in PowerShell are scripts that run at startup. Syntax to view your profile: `echo
Object Filtering
Filtering objects based on their properties is commonly done using Where-Object. Syntax: `Get-Process | Where-Object {_.WorkingSet -gt 100MB}`
Cmdlet Basics
Cmdlets are built-in PowerShell functions that perform an action. Syntax: `Get-Command`, `Set-Location C:\`
Loops: Do-While
Do-While loops execute the block at least once and then repeat as long as a condition is true. Syntax: `do { ... } while (
Loops: Foreach
Foreach loops iterate over each item in a collection. Syntax: `foreach (collection) { Write-Host item }`
String Operators
Strings can be manipulated with operators like -concat, -join, -split. Syntax for join: `'-'.join('a', 'b', 'c')`
Format Output
PowerShell has several cmdlets to format output, such as Format-Table, Format-List. Syntax: `Get-Process | Format-Table -Property ID, CPU, Name`
Script Parameters
Script parameters allow passing arguments to a PowerShell script from the command line. Syntax: `param ([type]param2)`
Logical Operators
Logical operators -and, -or, -not are used to combine multiple conditions. Syntax: `condition2`
Arrays
Arrays hold a collection of items. You can define an array with @(). Syntax: `
Transactions
PowerShell supports transactions which allows for a group of commands to be treated as a single unit of work. Syntax: `Start-Transaction; ...; Complete-Transaction`
Outputting Data
Data can be output to the screen or to another command via Write-Host, Write-Output, or simply using the object. Syntax: `Write-Host 'Hello World!'`
Remote Management
PowerShell enables management of remote systems via cmdlets like Invoke-Command, Enter-PSSession. Syntax: `Invoke-Command -ScriptBlock { ... } -ComputerName Computer`
Jobs
Jobs in PowerShell allow for asynchronous script execution. Syntax to start a job: `Start-Job -ScriptBlock { ... }`
Comments
Comments are used to add descriptive text in the script that is ignored during execution. Single-line Syntax: `# Comment`; Multi-line Syntax: `<# Comment #>`
Regular Expressions
Regular expressions are used for pattern matching. Syntax: `'`
Error Handling: Try-Catch
Try-Catch blocks are used for catching and handling exceptions. Syntax: `try { ... } catch { ... }`
Parameters
Parameters are variables used to pass data into functions. Syntax: `function MyFunction([type]param2) { ... }`
Operators: Assignment
Assignment operators are used to assign values to variables. The most common one is =. Syntax: `
File Operations
PowerShell can perform file operations with cmdlets like Get-Content, Set-Content. Syntax: `Get-Content file.txt`
Script Blocks
Script blocks enclose a collection of statements or expressions that can be passed as a unit of work. Syntax: `
Pipelines
Pipelines pass the output of one cmdlet as the input to another. Syntax: `Get-Process | Where-Object {_.CPU -gt 100}`
Comparison Operators
PowerShell provides operators for comparing values, such as -eq (equals), -ne (not equals), -gt (greater than), etc. Syntax: `
Serialization
Serialization converts objects to a format that can be stored or transmitted. PowerShell uses cmdlets like Export-Clixml. Syntax: `Get-Process | Export-Clixml -Path processes.xml`
Error Handling: Throw
The Throw statement is used to generate a terminating error. Syntax: `throw 'Error message'`
© Hypatia.Tech. 2024 All rights reserved.