Home » Tutorials


CodeDigest Navigation

Technology News
No News Feeds available at this time.

Community News
No News Feeds available at this time.
Windows PowerShell Cmdlet Tutorials with Example
Author: balamurali balaji

This tutorial is brought to you by BB Systems.

Windows PowerShell Cmdlet Tutorials with Example



Windows PowerShell is a task-based command-line shell and scripting language that helps IT professionals for windows Administrators. It helps to automate windows OS and applications.


In other words, Windows PowerShell a command-line utility useful for system administrators and manages to perform and check system level attributes and settings. It consists of cmdlets, functions, filters, scripts, aliases, and executables. PowerShell is based on the .NET Framework, and hence it seamlessly supports .NET base class library to integrate with the user in the command mode.


Out of the box, Windows PowerShellver 1.0 has already been available in the industry i am going to bring you a PS command and its usage and I hope that readers would allocate some time to read and use it at their leisure. It’s a sort of learning by fun!


The main ingredients of PowerShell are Aliases, Cmdlets, Providers(FileSystem, Security System, EventLog, Windows Intrumentation)


The built-in commands with Windows Powershell are called Cmdlets. A cmdlet is a single-feature, built-in command  by which an operation is completed.  Cmdlets include even the much familiar command line DOS commands.


To execute commands, click on the Windows PowerShell program in the Start menu that opens up a command window. Type the command and press enter.


[For those who have not installed PowerShell in their systems, you can get the downloaded here.


To be more clear and simple, the full syntax of the command is not going to be included. Learning by Example!


This tutorial will help us understand all the available Powershell commands available.




This command is used to add content to any existing item or file. ac stands for add content.




The commands below adds a text to the specified file.


PS D:\Users\bala> ac  bb.txt -value "Welcome PowerShell Add content command"

PS D:\Users\bala> ac  bb.txt -value "Welcome PowerShell"

PS D:\Users\bala> ac  documents\my_training_topics.txt -value  "Windows PowerShell"





Windows Services! Imagine where you go in your desktop to find the right menu or command to open up that Services Dialog listing all of those server components and their running status.


With Windows PowerShell, you just do it right away with your command windows and type in few words(cmdlet or mantra), you get a full control over starting, stopping or any action on a specified service component.




This command is used to start one or more than one stopped services. Alternatively, you can type start-service instead of sasv.


The Start-Service cmdlet sends a start message to the Windows Service Controller for each of the specified services. If a service is already running, the message is ignored without error. You can specify the services by their service names or display names, or you can use the InputObject parameter to supply a service object representing the services that you want to start.




The commands below starts fax service in variety of ways.


PS D:\Users\bala> start-service   -name Fax


PS D:\Users\bala> start-service   -displayname Fax


PS D:\Users\bala> start-service Fax


PS D:\Users\bala> sasv  Fax




   The select-string cmdlet identifies patterns in strings using the value of the Pattern parameter as a regular expression and matches input against it. You may use SimpleMatch parameter also to find the string specified in the Pattern parameter as a substring of the input.


   The cmdlet is also used to search string content from files.




By specifying the path and pattern, you can get a list of files and content that matches the pattern.


PS D:\Users\bala> select-string -path documents\*.txt  -pattern ".net"


documents\My_training_topics.txt:3:C# and .NET Framework 2.0, 3.0

documents\My_training_topics.txt:4:ASP.NET 2.0 and AJAX

documents\vs.netVSvb6.txt:5:As you all know, Visual studio .NET has gone through different versions and today with its

v3.5 wearing all sorts of hats like colorful WPF, vibrant workflows, connecting Services(WCF) and alerting AJAX,  has d

efinitely leaped into a strong and steady place in the minds of developing community. The easeness and integration of d

ifferent technologies has come to a new level in the new version of Visual Studio and it is for sure that .NET users, i

n particular, will benefit most of it.

documents\vs.netVSvb6.txt:9:But the legacy Visual Basic 6.0 doesn't seemed to leave VS as it is and still striving for

a better adaptation and prominent place in the IDE. And also, Visual Studio doesn't seemed to forget its ancestor mantr

as and always willing to give a place for the Guru. Many developers need not have time to think about this shake-hand r

elationship between VS.NET and VB6. Whether is it for VB6 battling to find a place in VS.NET, or VS.NET needs the Guru

for the guidance, developers are happy to see them walking together all around the technical spots in 2008, after almos

t half a decade.

documents\vs.netVSvb6.txt:31:<h2>Note</h2> This control works only when the printer is connected to the system network/



Given a list of strings as input, you can filter them by specifying the pattern.


PS D:\Users\bala> "VB.NET", "ASP.NET", "XML", "C#.NET" | select-string -pattern ".net"






Redirect the output of the dir command and then filter only files and folders starting with "Do".


PS D:\Users\bala> dir | select-string "Do"








       The Test-Path cmdlet determines whether all elements of the path exist. It returns "true" ($true) if all elements exist and "false" ($false) if any are missing.


    The main parameters are  -path, that specifies a path to be tested and -pathType that determines the type of element that the path locates. Returns TRUE if the element is of the specified type and FALSE if it is not.


        Valid values are:

        -- Container: An element that contains other elements, such as a directory or registry key.


        -- Leaf: An element that does not contain other elements, such as a file or registry entry.


        -- Any: Either a container or a leaf.

        The following lists the acceptable values for this parameter:


    The -isValid parameter determines whether the syntax of the path is correct.






The following command checks if d:\bala folder exists.


PS D:\Users\bala> test-path -path d:\bala



The following command check if d:\bala\downloads folder exists

PS D:\Users\bala> test-path -path d:\bala\downloads



The following cmdlet checks if d:\bala\documents exists.

PS D:\Users\bala> test-path -path d:\bala\documents



The following cmdlet checks if any file in d:\bala\documents folder except with .doc extension

PS D:\Users\bala> test-path  d:\bala\documents\* -exclude "*.doc"



The following cmdlet checks if a given path is a file

PS D:\Users\bala> test-path  d:\bala\documents -pathtype leaf



The following cmdlet checks if a given path is a folder.

PS D:\Users\bala> test-path  d:\bala\documents -pathtype container




Recent Articles



The diff is an alias used for compare-object cmdlet. It compares two sets of objects. One set of objects is the Reference set and the other is the Difference set.


The result of the comparison indicates whether a property value appeared only in the object from the Reference set (indicated by the <= symbol), only in the object from the Difference set (indicated by the => symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).


The two main parameters are -referenceObject, objects used as a reference for comparison and  -differenceObject, objects to compare to those specified as reference objects.


The -includeEqual parameter displays characteristics of compared objects that are equal.





The following command compares the contents of two text files and displays only those lines which appear in one or the other

file, but not in both files.


PS D:\Users\bala> diff -referenceobject $(get-content documents\data2.txt) -differenceobject $(get-content documents\dat



InputObject                                                 SideIndicator

-----------                                                            -------------

XML, XPATH XSL and XSD                                    =>

Object-Oriented Design and Analysis                            =>

Advanced Excel and VBA                                           =>

Windows Server 2008                                                =>

Sql Server 2008                                                        =>

Windows PowerShell                                                <=



PS D:\Users\bala> diff -referenceobject $(get-content documents\data1.txt) -differenceobject $(get-content documents\dat



InputObject                                                 SideIndicator

-----------                                                            -------------

Windows PowerShell                                                 =>

XML, XPATH XSL and XSD                                    <=

Object-Oriented Design and Analysis                            <=

Advanced Excel and VBA                                          <=

Windows Server 2008                                               <=

Sql Server 2008                                                        <=


The following command displays all lines of content from both files indicating whether each line appears in only data1.txt or

data2.txt or appears in both of the files.


PS D:\Users\bala> diff -referenceobject $(get-content documents\data1.txt) -differenceobject $(get-content documents\dat

a2.txt) -includeequal


InputObject                                                 SideIndicator

-----------                                                         -------------

C# and .NET Framework 2.0, 3.0                              ==

ASP.NET 2.0 and AJAX                                          ==

SQL Server 2000 and 2005 new features                       ==

Design Patterns                                                       ==

Unified Modeling Language                                       ==

RDBMS concepts                                                    ==

WebServices and Remoting                                       ==

Windows PowerShell                                               =>

XML, XPATH XSL and XSD                                   <=

Object-Oriented Design and Analysis                           <=

Advanced Excel and VBA                                         <=

Windows Server 2008                                              <=

Sql Server 2008                                                      <=


The following sequence of cmdlets compares two sets of process objects. one before running notepad and another after running notepad.


PS D:\Users\bala> $before = get-process

PS D:\Users\bala> notepad

PS D:\Users\bala> $after = get-process

PS D:\Users\bala> diff -referenceobject $before -differenceobject $after


InputObject                                                 SideIndicator

-----------                                                           -------------

System.Diagnostics.Process (notepad)                        =>





The measure-command measures the time it takes to run script blocks and cmdlets.It runs a script block or cmdlet internally,

times the execution of the operations and returns the execution time.



You need to supply a script block that contains other cmdlets within a curly braces{} in the  -expression parameter.




The following command measures the time it takes to run a "get-eventlog" command.


PS D:\Users\bala> measure-command {get-eventlog}


cmdlet get-eventlog at command pipeline position 1

Supply values for the following parameters:

LogName: application


Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 11

Milliseconds      : 161

Ticks             : 111613657

TotalDays         : 0.00012918247337963

TotalHours        : 0.00310037936111111

TotalMinutes      : 0.186022761666667

TotalSeconds      : 11.1613657

TotalMilliseconds : 11161.3657


The following command gives the execution time for dir command.

PS D:\Users\bala> measure-command {dir}



Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 0

Milliseconds      : 222

Ticks             : 2226067

TotalDays         : 2.57646643518519E-06

TotalHours        : 6.18351944444444E-05

TotalMinutes      : 0.00371011166666667

TotalSeconds      : 0.2226067

TotalMilliseconds : 222.6067




This cmdlet is used to read a line of input from the console.It can be used to prompt for input from a user or to create secure strings.

The parameter-prompt specifies the string that will become the prompt object. When you set true to the -asSecureString, the input will be echoed as star characters (*). The output will then be a Securestring object.


This command presents the string "Enter the user id:" as a prompt and the entered value is stored in the $name variable.

PS D:\Users\bala> $name = read-host "Enter the user id:"

Enter the user id:: bbmurali_2000

The follwing cmdlet accepts a password as a securestring and store it in the $password variable.

PS D:\Users\bala> $password = read-host "Enter the password" -assecurestring

Enter the password: *******