Powershell Notes
Powershell Notes
https://youtu.be/UVUd9_k9C6A?t=16290
Lab at pg. 58
Current point in notes
• 1.0
Set-StrictMode - Version 2 -Prohibits references to uninitialized variables, except for uninitialized variables in strings.
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]
-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.
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
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
<string[ ]> - The [ ] shows that this Input can accept an array, collection, or list of strings. You can provide a single value, or multiple.
Examples:
Server-R2
Files02
Files03
DC04
DC03
This allows us to run commands against entire sets of computers. (Seperates User Workstations)
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