Tag Archives: Powershell

Syntax in PowerShell

Get-EventLog [-LogName] <String> [[-InstanceId] <Int64[]>] [-After <DateTime>] [-AsBaseObject] [-Before <DateTime>] [-ComputerName <String[]>] [-EntryType {Error | Information | FailureAudit | SuccessAudit | Warning}] [-Index <Int32[]>] [-Message <String>] [-Newest <Int32>] [-Source <String[]>] [-UserName <String[]>]
Get-EventLog [-AsString] [-ComputerName <String[]>] [-List] [<CommonParameters>]
  • There are two possible combinations of parameters you can’t mix between them, some commands may have more than 2 sets.
  • Anything in square brackets is optional.
  • If you omit optional parameter names then they MUST be provided in the correct order! (e.g. ‘Get-EventLog Security 10’ is shorthand for  ‘Get-EventLog -LogName Security -InstanceID 10’. ‘Get-EventLog 10 Security’ will not work as there is no Event Log called 10).
[-LogName] <String>
  • The log name is not optional as its not in square brackets but actually writing the word LogName is!
[[-InstanceId] <Int64[]>]
  • Instance ID is optional (the whole thing is in square brackets
  • Writing ‘-InstanceID’ is optional
  • InstanceID expects a 64-bit integer (-9223372036854775808 to 9223372036854775807) and the additional square brackets indicate it will accept and array or list of them
  • This is a switch, essentially a parameter without a value
  • These are a bunch of parameters available to ALL commands

Alias in PowerShell

Finding the alias of a command:

get-alias -Definition "Get-Service"

Finding the command of an alias

Get-alias gsv
Help gsv

Finding the alias of a parameter (getting the alias of the computername parameter for ‘Get-EventLog’):

(get-command get-eventlog | select -ExpandProperty parameters) .computername.aliases

Finding the parameter of an alias (getting the parameter of the -Cn alias for ‘Get-EventLog’):

get-eventlog -Cn

(this will error with the error displaying the full parameter name of the Alias)

Objects in Powershell

$List = New-Object System.Collections.Generic.List[System.Object]

foreach($User in $Users){

    ### Your processing code goes here ###

    $props = @{
        UPN = $User.User
        Type = $Type
    $object = new-object psobject -Property $props

This is my second version of this as I more often want to create custom objects in a loop as the standard properties are missing something or I want to add a value from another query.
I’ve used $Users here as my input array as that’s what I most commonly have to use.

I’ve used a list here instead of an array as they are faster. You can append to a list. To add to an array you actually have to create an array one larger than the last array, populate it with the previous values and the the one you want to add.

Further reading: https://social.technet.microsoft.com/wiki/contents/articles/7804.powershell-creating-custom-objects.aspx

WSUS Commands

wuauclt /detectnow



WSUS Commands in Windows 10 / Server 2016:

usoclient StartScan

WSUS in PowerShell:

Windows 10 1809 Optional Features (inc RSAT)

Windows 10 from 1809 onward, pulls optional features from WSUS so will show no available updates unless they are configured.

To get around this you can configure a PC to search online for available optional features…

Run “gpedit.msc” to edit your local computer policy The setting in question is: Computer Configuration\Administrative Templates\System\Specify settings for optional component installation and component repair

My local policy seems to have defaulted to “Disabled” – after changing it to “Enabled” and selecting the checkbox labeled “Download repair content and optional features directly from Windows Update instead of Windows Server Update Services (WSUS)” the RSAT tools installed for me.

Then you can powershell to get the updates you need e.g.:

Add-WindowsCapability –online –Name “Rsat.ActiveDirectory.DS-LDS.Tools~~~~”

Check out this link for lots more options!