PowerShell commands for Beginners

PowerShell is the de-facto tool to automate the day to day task in windows environment and however, Microsoft is now providing the PowerShell for all the operating systems. To help to start with, this blog post will give you a list of PowerShell commands for beginners.

To install PowerShell core in Linux/macOS, please refer to this article.

In Windows, by default you can see PowerShell and PowerShell ISE. PowerShel is more to run the commands interactively, whereas PowerShell ISE provides the environment to develop and test the scripts.

Important commands that every beginners should know:

Know the PowerShell version:

To know the version of the PowerShell, run the following command on PowerShell or PowerShell ISE.

$PSVersionTable

and the output is something shown below.

PowerShell version

The above command tells you what PowerShell version are you running. Currently, PowerShell version 7.* is available. To know more about the versions refer to this article. Based on the version that you are using, you will get the supported functionality.

Know the PowerShell commands available:

To know the commands that are loaded in your PowerShell session, run the following command.

# To know the commands loaded in the PowerShell session
Get-Command

# To know the commands loaded in the PowerShell session and the available commands thet are not loaded
Get-Command -ListAvailable 

Understand the command:

Once you get the list of commands available, now try to understand the command and its syntax as below. If you are not sure how to use the command, now get the help as below.

PS> Get-help -Name <command name>

#Example to get the details of Get-Process Command
Get-Help -Name Get-Process

Output:
PS> Get-Help -Name Get-Process

NAME
    Get-Process
    
SYNTAX
    Get-Process [[-Name] <string[]>] [-Module] [-FileVersionInfo] [<CommonParameters>]
    
    Get-Process [[-Name] <string[]>] -IncludeUserName [<CommonParameters>]
    
    Get-Process -Id <int[]> -IncludeUserName [<CommonParameters>]
    
    Get-Process -Id <int[]> [-Module] [-FileVersionInfo] [<CommonParameters>]
    
    Get-Process -InputObject <Process[]> [-Module] [-FileVersionInfo] [<CommonParameters>]
    
    Get-Process -InputObject <Process[]> -IncludeUserName [<CommonParameters>]

# To get the help of the command with examples
PS> Get-Help -Name Get-Process -Examples

Note: If you do not get the help, try running the command “Update-Help

Understand the output of the command:

Once you execute the command, you need to understand the output of the command. The output of a PowerShell command is always an object or array of objects that have some properties and methods.

Note: If you are not aware of PowerShell objects, please follow this article.

To know what properties a PowerShell command output has, run the following command.

PS> Get-Item ./django-employee-app/ | Get-Member
Output:
   TypeName: System.IO.DirectoryInfo
Name                      MemberType     Definition
----                      ----------     ----------
Create                    Method         void Create()
CreateSubdirectory        Method         System.IO.DirectoryInfo CreateSubdirectory(string path)
Delete                    Method         void Delete(), void Delete(bool recursive)
Equals                    Method         bool Equals(System.Object obj)
GetDirectories            Method         System.IO.DirectoryInfo[] GetDirectories(), 
GetFiles                  Method         System.IO.FileInfo[] GetFiles(), 
GetFileSystemInfos        Method         System.IO.FileSystemInfo[] 
GetHashCode               Method         int GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         void 
GetType                   Method         type GetType()
InitializeLifetimeService Method         System.Object InitializeLifetimeService()
MoveTo                    Method         void MoveTo(string destDirName)
Refresh                   Method         void Refresh()
ToString                  Method         string ToString()
PSChildName               NoteProperty   string PSChildName=django-employee-app
PSDrive                   NoteProperty   PSDriveInfo PSDrive=/
PSIsContainer             NoteProperty   bool PSIsContainer=True
PSParentPath              NoteProperty   string 
PSPath                    NoteProperty   string 
PSProvider                NoteProperty   ProviderInfo 
Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
CreationTime              Property       datetime CreationTime {get;set;}
CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}
Exists                    Property       bool Exists {get;}
Extension                 Property       string Extension {get;}
FullName                  Property       string FullName {get;}
LastAccessTime            Property       datetime LastAccessTime {get;set;}
LastAccessTimeUtc         Property       datetime LastAccessTimeUtc {get;set;}
LastWriteTime             Property       datetime LastWriteTime {get;set;}
LastWriteTimeUtc          Property       datetime LastWriteTimeUtc {get;set;}
Name                      Property       string Name {get;}
Parent                    Property       System.IO.DirectoryInfo Parent {get;}
Root                      Property       System.IO.DirectoryInfo Root {get;}
BaseName                  ScriptProperty System.Object BaseName {get=$this.Name;}

Observe the highlighted text where it is showing the properties that directory have.

The above are the basic commands for the beginner to start digging into more concepts and usage of the PowerShell commands.

Hope you find the article useful.

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *