Logo
Pattern

Discover published sets by community

Explore tens of thousands of sets crafted by our community.

Basic PowerShell Scripting

37

Flashcards

0/37

Still learning
StarStarStarStar

Events

StarStarStarStar

PowerShell can respond to system events using Register-ObjectEvent. Syntax: `Register-ObjectEvent -InputObject timerEventNameElapsedActionWriteHostTimerelapsed!timer -EventName Elapsed -Action { Write-Host 'Timer elapsed!' }`

StarStarStarStar

Variables

StarStarStarStar

Variables store data that can be used and manipulated throughout a PowerShell script. Syntax: `variableName=valuevariableName = 'value'`

StarStarStarStar

HashTables

StarStarStarStar

HashTables are a collection of key-value pairs and are created using @{}. Syntax: `myHashtable=@Key1=Value1;Key2=Value2myHashtable = @{Key1 = 'Value1'; Key2 = 'Value2'}`

StarStarStarStar

Functions

StarStarStarStar

Functions are blocks of code designed to perform a particular task and can be reused. Syntax: `function Verb-Noun { ... }`

StarStarStarStar

Modules

StarStarStarStar

Modules are packages of related PowerShell cmdlets, functions, variables, and more. Syntax to import a module: `Import-Module ModuleName`

StarStarStarStar

Sorting Objects

StarStarStarStar

Cmdlet Sort-Object sorts objects by property values. Syntax: `Get-Process | Sort-Object -Property CPU`

StarStarStarStar

Providers

StarStarStarStar

Providers allow access to data stores, such as the file system, registry, or environment variables. Syntax: `Get-Item -Path Env:Path`

StarStarStarStar

Data Types

StarStarStarStar

PowerShell supports various data types like integers, strings, booleans, and arrays. Syntax for array: `array=@(1,2,3)array = @(1, 2, 3)`

StarStarStarStar

Operators: Arithmetic

StarStarStarStar

Arithmetic operators include +, -, *, /, % for calculations. Syntax: `sum=sum = first + secondsecond`

StarStarStarStar

Conditional Statements

StarStarStarStar

Conditional statements control the flow of execution based on conditions. Syntax: `if(condition)...elseif(condition) { ... } elseif(condition) { ... } else { ... }`

StarStarStarStar

Loops: For

StarStarStarStar

For loops execute a block of code a set number of times. Syntax: `for (i=0;i = 0; i -lt 10; i++) { Write-Host i }`

StarStarStarStar

Loops: While

StarStarStarStar

While loops continue executing as long as a condition is true. Syntax: `while (condition)...condition) { ... }`

StarStarStarStar

PowerShell Profiles

StarStarStarStar

Profiles in PowerShell are scripts that run at startup. Syntax to view your profile: `echo PROFILEPROFILE`

StarStarStarStar

Object Filtering

StarStarStarStar

Filtering objects based on their properties is commonly done using Where-Object. Syntax: `Get-Process | Where-Object {_.WorkingSet -gt 100MB}`

StarStarStarStar

Cmdlet Basics

StarStarStarStar

Cmdlets are built-in PowerShell functions that perform an action. Syntax: `Get-Command`, `Set-Location C:\`

StarStarStarStar

Loops: Do-While

StarStarStarStar

Do-While loops execute the block at least once and then repeat as long as a condition is true. Syntax: `do { ... } while (condition)condition)`

StarStarStarStar

Loops: Foreach

StarStarStarStar

Foreach loops iterate over each item in a collection. Syntax: `foreach (iteminitem in collection) { Write-Host item }`

StarStarStarStar

String Operators

StarStarStarStar

Strings can be manipulated with operators like -concat, -join, -split. Syntax for join: `'-'.join('a', 'b', 'c')`

StarStarStarStar

Format Output

StarStarStarStar

PowerShell has several cmdlets to format output, such as Format-Table, Format-List. Syntax: `Get-Process | Format-Table -Property ID, CPU, Name`

StarStarStarStar

Script Parameters

StarStarStarStar

Script parameters allow passing arguments to a PowerShell script from the command line. Syntax: `param ([type]param1,[type]param1, [type]param2)`

StarStarStarStar

Logical Operators

StarStarStarStar

Logical operators -and, -or, -not are used to combine multiple conditions. Syntax: `condition1andcondition1 -and condition2`

StarStarStarStar

Arrays

StarStarStarStar

Arrays hold a collection of items. You can define an array with @(). Syntax: `myArray=@(1,two,3)myArray = @(1, 'two', 3)`

StarStarStarStar

Transactions

StarStarStarStar

PowerShell supports transactions which allows for a group of commands to be treated as a single unit of work. Syntax: `Start-Transaction; ...; Complete-Transaction`

StarStarStarStar

Outputting Data

StarStarStarStar

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!'`

StarStarStarStar

Remote Management

StarStarStarStar

PowerShell enables management of remote systems via cmdlets like Invoke-Command, Enter-PSSession. Syntax: `Invoke-Command -ScriptBlock { ... } -ComputerName Computer`

StarStarStarStar

Jobs

StarStarStarStar

Jobs in PowerShell allow for asynchronous script execution. Syntax to start a job: `Start-Job -ScriptBlock { ... }`

StarStarStarStar

Comments

StarStarStarStar

Comments are used to add descriptive text in the script that is ignored during execution. Single-line Syntax: `# Comment`; Multi-line Syntax: `<# Comment #>`

StarStarStarStar

Regular Expressions

StarStarStarStar

Regular expressions are used for pattern matching. Syntax: `stringmatch[azAZ]+string -match '^[a-zA-Z]+'`

StarStarStarStar

Error Handling: Try-Catch

StarStarStarStar

Try-Catch blocks are used for catching and handling exceptions. Syntax: `try { ... } catch { ... }`

StarStarStarStar

Parameters

StarStarStarStar

Parameters are variables used to pass data into functions. Syntax: `function MyFunction([type]param1,[type]param1, [type]param2) { ... }`

StarStarStarStar

Operators: Assignment

StarStarStarStar

Assignment operators are used to assign values to variables. The most common one is =. Syntax: `var=5var = 5`

StarStarStarStar

File Operations

StarStarStarStar

PowerShell can perform file operations with cmdlets like Get-Content, Set-Content. Syntax: `Get-Content file.txt`

StarStarStarStar

Script Blocks

StarStarStarStar

Script blocks enclose a collection of statements or expressions that can be passed as a unit of work. Syntax: `...{ ... }`

StarStarStarStar

Pipelines

StarStarStarStar

Pipelines pass the output of one cmdlet as the input to another. Syntax: `Get-Process | Where-Object {_.CPU -gt 100}`

StarStarStarStar

Comparison Operators

StarStarStarStar

PowerShell provides operators for comparing values, such as -eq (equals), -ne (not equals), -gt (greater than), etc. Syntax: `vareqvaluevar -eq 'value'`

StarStarStarStar

Serialization

StarStarStarStar

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`

StarStarStarStar

Error Handling: Throw

StarStarStarStar

The Throw statement is used to generate a terminating error. Syntax: `throw 'Error message'`

Know
0
Still learning
Click to flip
Know
0
Logo

© Hypatia.Tech. 2024 All rights reserved.