














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive overview of powershell fundamentals, covering topics such as pipelines, network configuration cmdlets, data conversion techniques, and error handling. It includes practical examples and explanations to help users understand and apply these concepts effectively. The document also explores advanced features like powershell remoting and background jobs, enhancing the user's ability to manage and automate tasks.
Typology: Cheat Sheet
1 / 22
This page cannot be seen from the preview
Don't miss anything!
Pipeline in PowerShell The pipeline (|) in PowerShell is a feature used to pass the output of one command as input to another command. It allows chaining commands together, enabling efficient data manipulation and automation.
Running W32Time Windows Time Running Spooler Print Spooler
chrome 124.
These examples demonstrate how to use pipelines effectively for different tasks. Let me know if you need further assistance!
NETWORK CONFIGURATION CMDLETS Network Configuration Cmdlets in PowerShell These cmdlets help manage network settings like IP addresses, DNS servers, routing tables, and firewall rules. They enable configuration, troubleshooting, and automation of network tasks on Windows systems. Managing IP Addresses---- Cmdlets to view, assign, or remove IP addresses on network adapters. New-NetIPAddress Creates a new IP address
Cmdlets to create or modify firewall rules controlling network traffic.
PowerShell scripting is the use of PowerShell commands and scripts to automate tasks, manage systems, and configure settings on Windows and other systems through a command- line interface. POWERSHELL ADVANTAGES AND DISADVANTAGES Advantages of PowerShell:
CONVERT DATA TO CSV HTM JSON In this response, I'll demonstrate how to convert data into three different formats—CSV, HTML, and JSON—using PowerShell scripting. I'll provide short and simple PowerShell code examples for each format, along with the corresponding output.
Explanation: Export data into a CSV file using Export-Csv. Code: $data = @([PSCustomObject]@{Name="John"; Age=30}, [PSCustomObject]@{Name="Jane"; Age=25}) $data | Export-Csv - Path "data.csv" - NoTypeInformation Output (data.csv): "Name","Age" "John", "Jane",
Name | Age |
---|---|
John | 30 |
Jane | 25 |
$array = $array | Where-Object { $_ - ne 3 }
$array[0]
Manipulating Hash Tables: $hashTable = @{ "Name" = "John"; "Age" = 30 } $hashTable["Country"] = "USA"
$hashTable.Remove("Age")
$hashTable["Name"]
Using Arrays with Hash Tables: $array = @("Apple", "Banana", "Cherry") $hashTable = @{} for ($i = 0; $i - lt $array.Length; $i++) { $hashTable[$i] = $array[$i] } $hashTable[1]
WMI (Windows Management Instrumentation): WMI is a Windows-based framework that allows access to system management information such as hardware, software, and network settings. CIM (Common Information Model): CIM is a standard model for representing and accessing system information across platforms, providing a more modern and efficient way to query system data.
Querying System Information Using WMI: Command: Get-WmiObject - Class Win32_OperatingSystem Output: PSComputerName :. CSName : USER-PC Caption : Microsoft Windows 10 Pro OSArchitecture : 64-bit Version : 10.0.
Set-Location - Path "HKCU:\Software\Scripts" Example Output: PS HKCU:\Software\Scripts>
PSDriveName MyPSDrive
MyPSDrive This sequence shows how to navigate, create keys, set properties, and verify them using PowerShell with outputs for each step.
UNIT 3------------------------------------------------------------- CONDITIONAL STATEMENTS (IF/ELSE,ELSEIF,NESTED IF/ELSE) in PowerShell scripting, conditional statements control the flow of execution based on specified conditions. The primary conditional constructs are if, elseif, and else.
Write-Output "Number is greater than 5" } Output: Number is greater than 5
Output: 01234
These loops help automate repetitive tasks in PowerShell scripting.
TRY CATCH FINALLY (MOST IMPORTANT) In PowerShell, try, catch, and finally blocks are used for error handling. Here's an explanation with code and output:
$number = 10 / 0 } catch {
Write-Host "An error occurred: $_" } finally {
Write-Host "This is the finally block." } Output: An error occurred: Attempted to divide by zero. This is the finally block. Explanation: The try block attempts to divide 10 by 0, which causes an error (Attempted to divide by zero). The catch block catches this error and prints the error message. The finally block always executes, printing its message, even though an error occurred. ErrorAction Parameter - Most PowerShell cmdlets have an - ErrorAction parameter that controls what happens when an error occurs. Some common options are - Continue - The default, it shows the error message but keeps running the script. Stop - Stops running the script when an error is encountered. SilentlyContinue - Suppresses the error message and continues. Inquire - Asks what you want to do each time an error occurs. ErrorAction Parameter - Most PowerShell cmdlets have an - ErrorAction parameter that controls what happens when an error occurs. Some common options are – Continue - The default, it shows the error message but keeps running the script. Stop - Stops running the script when an error is encountered. SilentlyContinue - Suppresses the error message and continues. Inquire - Asks what you want to do each time an error occurs.
Invoke-Command - ComputerName Server1,Server2 - ScriptBlock { Get-Process }
Components of PowerShell Remoting
One-to-One vs. One-to-Many Remoting One-to-One Remoting Interactive session with one remote computer. Uses Enter-PSSession. Example use case: Troubleshooting on a specific server. One-to-Many Remoting
Execute commands on multiple computers at once. Uses Invoke-Command. Example use case: Deploying updates or configurations across servers.
UNIT 5------------------------------------------------- BACKGROUND JOBS-USING BG JOB-STARTING AND MANAGING JOBS-USING SCHEDULED JOB (NA)-CREATING A SCHEDULED JOB Background Jobs in PowerShellBackground jobs in PowerShell allow processes to run asynchronously in the background, enabling other tasks to continue running without interruption. They are useful for multitasking and improving efficiency in scripts.
Start-Job Start-Job initiates a background job, running a script or command in the background. It does not block the session, allowing the user to perform other tasks while the job is running.
Get-Job Get-Job lists all background jobs that have been started, displaying information such as the job ID, its current status (running, completed, etc.), and other details about the job.
Receive-Job Receive-Job retrieves the results of a completed background job. This cmdlet allows you to collect any output or data generated by the job once it has finished.
Stop-Job