How to find correct PowerShell command

Many of our students always ask how can I remember all the commands in the PowerShell? Well I don’t know all the commands and I don’t remember all the commands. Why? Should I? Short answer is NO.

Why? Because depending of the PowerShell version there is thousands of commands, and I have a life (believe it or not, I do)

The correct question should be

How can we find a correct PowerShell command and how to use it.

I will go through couple of ways of how to discover PowerShell commands with Get-Verb, Get-Command, and Get-Help.

I’m using Windows PowerShell version 5. In this example scenario you can see how to block Smb Share access for Everyone users group.

PowerShell command structure

PowerShell command always starts with verb, like:

Get, Set, Remove and so on

After verb next in place is line letter and last one is noun like “ChildItem, Computer, Mailbox”.

So whole command looks like:

 Get-Computer

Discovering Verbs

In PowerShell version 3 (and later) there is command that can be used to discover all verbs Get-Verb. Get-Verb command outputs following table

ps_verbs.png

As you can see Get-Verb command prints out two properties, Verb itself and Group.

Windows PowerShell Verbs are group based on their most common use. The groups are designed to make the verbs easy to find and compare.

Each Windows PowerShell verb is assigned to one of the following groups. 

  • Common: Define generic actions that can apply to almost any cmdlet,  such as Add, Get, Set, Remove, Delete, Update

  • Data: Define actions that apply to data handling, such as Backup

  • Lifecycle: Define actions that apply to the lifecycle of a cmdlet, such as Complete

  • Diagnostic: Define actions that apply to diagnostics, such as Debug

  • Communications: Define actions that apply to communications, such as Connect

  • Security: Define actions that apply to security, such as Revoke.

  • Other: Define other types of actions

With Get-Verb command you can’t just use group name for filtering. You need to pipe output of Get-Verb command to the Where-Object command with Security group name: Get-Verb | where-object {$_.Group -eq “Security”} 

ps_get-verb.png

Now you have discovered that there is Block verb and that is probably our required verb what you need to use to block access to file share.

Discovering commands

Now you need to get all available commands from Verb Block. This can be done using Get-Command command and adding –Verb parameter like this: Get-Command -Verb Block

PS_Discover_Command.png

As you can see in the output there is command Block-SmbShareAccess and It’s source is SmbShare. So this could be correct command to achieve your task to block access to SmbShare.

How to use command based help?

Now need to use Get-Help command to see how to use Block-SmbShareAccess like this: Get-Help Block-SmbShareAccess

ps_get-help.png

In the Synopsis section you can see that Block-SmbShareAccess command is used to add deny access control entry to the Smb Share.

As in syntax section you can see that parameter “Name and AccountName” are in between single square brackets this means that those parameters are mandatories. Of course you can use parameter –Full or –ShowWindow to see full help.

Now you know mandatory parameters so you can start to build a command which is following:

Block-SmbShareAccess -Name “FileShare1” -AccountName “Everyone”

ps_block-smbshareaccess.png

After this file share access through Everyone users group is blocked on FileShare1.