0% found this document useful (0 votes)
2 views

Powershell Notes

The document contains detailed notes on PowerShell, including strict mode settings, parameter usage, and examples of commands. It explains the importance of using the correct syntax and parameters when executing commands, as well as how to handle multiple values and import data from text files. Additionally, it introduces the concept of pipelines in PowerShell, demonstrating how to connect cmdlets for more efficient command execution.

Uploaded by

dallas123cowboys
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Powershell Notes

The document contains detailed notes on PowerShell, including strict mode settings, parameter usage, and examples of commands. It explains the importance of using the correct syntax and parameters when executing commands, as well as how to handle multiple values and import data from text files. Additionally, it introduces the concept of pipelines in PowerShell, demonstrating how to connect cmdlets for more efficient command execution.

Uploaded by

dallas123cowboys
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Quick Notes

Thursday, October 20, 2022 3:04 PM

https://youtu.be/UVUd9_k9C6A?t=16290
Lab at pg. 58
Current point in notes

Powershell Notes Page 1


Powershell Notes The Version parameter to determine which coding rules are enforced.
Wednesday, October 19, 2022 11:04 AM

• 1.0
Set-StrictMode - Version 2 -Prohibits references to uninitialized variables, except for uninitialized variables in strings.

Prevents typos from allowing string to run • 2.0

Example: Get-mailbox $usermane | Set-mailbox -Hiddenfromaddresslist:$true -Prohibits references to uninitialized variables. This includes uninitialized variables in strings.
See the typo ? I misspelt username. What do you think that would do? Far from erroring out,
-Prohibits references to non-existent properties of an object.
Powershell just inserts an empty string, then proceeds to get every single mailbox and then hide
them.
-Prohibits function calls that use the syntax for calling methods.

• 3.0
Ctrl + C Aborts any commands
[All 2.0 Rules]

-Prohibit out of bounds or unresolvable array indexes.

-Latest

-Selects the latest version available. The latest version is the most strict. Use this value to make sure that
scripts use the strictest available version, even when new versions are added to PowerShell.

Powershell Notes Page 2


Parameters - Help Guide All examples are from Help Get-EventLog in Powershell <-This helps you understand Parameters in a Parameter Set
Wednesday, October 19, 2022 3:13 PM

Get-EventLog [-LogName] <string> [[-InstanceId] <long[]>] [-ComputerName <string[]>] [-Newest <int>] [-After <datetime>] [-Before
-ComputerName <datetime>] [-UserName <string[]>] [-Index <int[]>] [-EntryType {Error | Information | FailureAudit | SuccessAudit | Warning}]
-LogName <--- These are Parameters [-Source <string[]>] [-Message <string>] [-AsBaseObject] [<CommonParameters>] Parameter Set 1
-Before -Cannot use Parameters that are unique from other sets
<value> <--- Input example
Get-EventLog [-ComputerName <string[]>] [-List] [-AsString] [<CommonParameters>] Parameter Set 2
Find the Optional, and Positional Parameters!

Brackets surrounding Parameter Name + Input = Optional Parameter Optional = Don't have to use at all
[-LogName <String>]
Brackets not surrounding entire Parameter (Name + Input) = Not Optional Not Optional = Have to use
-LogName <String>
Brackets surrounding Parameter Name = Positional Positional = Must be in the same position in the Parameter
(Must always be in the correct location in a parameter set)(Don't have to use Name with Value) Set
[-LogName] <String>
Brackets Surrounding entire Parameter + Parameter Name = Optional, Positional Not Optional, Positional = Don't have to use, must be in the
[[-LogName] <String[]>] same position

(You don't have to type Positional Parameters every time!!!)

Make sure to keep track of Positional Parameters Example of wrong Positional Parameters Cmdlet = Verb+Noun
Get-EventLog
Noun-Verb
Get-EventLog Application 0 Get-EventLog 0 Application
Is the same as Is the same as
Get-EventLog -LogName Application -InstanceId 0 Get-EventLog -LogName 0 -InstanceId Application

Get-EventLog System -Newest 20 Will not work because 0 is attached to -LogName and no Log is named 0
is the same as
Get-EventLog -LogName System -Newest 20

Powershell Notes Page 3


Parameters 2 - Multiple Values
Thursday, October 20, 2022 3:38 PM

Inputs String - A series of letters or numbers

Int, Int32, Int64 - An integer number

DateTime - Generally, date based on your computer settings

<string[ ]> - The [ ] shows that this Input can accept an array, collection, or list of strings. You can provide a single value, or multiple.

Examples:

Get-EventLog -LogName Security -ComputerName Server-R2


- One value supplied

Get-EventLog -LogName Security -ComputerName Server-R2, DC4, Files02


- Comma separated, multiple values

Get-EventLog -LogName Security -ComputerName 'Server-R2','Files02'


- Values with spaces must be enclosed in quotation marks. Although these values don't need it, it's still legal.

Get-EventLog -LogName Security -ComputerName 'Server-R2,Files02'


Not illegal, but the value will be read as Server-R2,Files02 leading to an incorrect ComputerName

Powershell Notes Page 4


Parameters 3 - Import .txt
Thursday, October 20, 2022 4:00 PM

Get-EventLog -LogName Application -ComputerName (Get-Content names.txt)


• Commands in parentheses will always execute first
• Get-Content will read the file

Example of inside the text file

Server-R2
Files02
Files03
DC04
DC03

This allows us to run commands against entire sets of computers. (Seperates User Workstations)

Powershell Notes Page 5


Pipeline Intro
Thursday, October 27, 2022 1:26 PM

| <--- This is a Pipe not to be confused with L or I **WARNING**


https://youtu.be/UVUd9_k9C6A?t=5658 Get-Service | Stop-Service
Get-Service | Select-Object name, status | Sort-Object name This does exactly what you think…Stops all Services
-Connects Cmdlets to produce better results (Think of it as multiple steps)
Butttt you can use
For example: Step 1 | Step 2 | Step 3 | Export-CSV -Path C:\service.csv Get-Service | Stop-Service -whatif
-You want to filter as far left as possible It will give you a preview of what would happen

Now you noticed the export cmdlet I used earlier, this lets you save your work for later
Get-Service | Stop-Service -confirm
use.
Gives you the option to select yes or no, and some other options per operation

Import-CSV C:\service.csv

Get-Service -name bits | get-member


Pipe to get-member to Shows properties and methods

Get-Service -name bits | gm


-Accomplishes the same thing using an 'alias' for get-member

Get-Service | Select-Object -Property name,status


-Allows you to see specific properties

Get-Service | Select -Property name,status


-Alias is Select, same as above

Get-childitem | select -property name, length | sort -Property length -descending


-Using a combination of cmdlets and parameters (With Aliases for comfort) across the
pipeline allows us to tailor this to our needs. Sort-Object was the cmdlet for above

Powershell Notes Page 6

You might also like