PowerShell: Break, Return Or Exit?


What is the difference between the keywords Exit, Return and Break in PowerShell?
What does each exactly do?

Short Answer

  • Break terminates execution of a loop or switch statement and hands over control to next statement after it.
  • Return terminates execution of the current function and passes control to the statement immediately after the function call.
  • Exit terminates the current execution session altogether. It also closes the console window and may or may not close ISE depending on what direction the wind is facing.

Not sure I get it yet…
Can you provide some examples?

Sure thing, let’s look at an example for each one.

Continue reading “PowerShell: Break, Return Or Exit?”


C#: How Can We Enumerate An Enum?


How can I enumerate an enum data type in C#?

Say I have this:

    enum PumpkinVariety

How can I list this dynamically in any useful way?

Short Answer

If you have a plain vanilla enum, and you don’t have any funky stuff there such as duplicated values, it’s as easy as this:

foreach (PumpkinVariety pv in Enum.GetValues(typeof(PumpkinVariety)))
    // do something here

If you do have funky stuff going on, that’s another matter…

Continue reading “C#: How Can We Enumerate An Enum?”

C#: string or String?


What’s the deal with the string and other types in C#? I see both string and String all over the place. Same goes with int and Int32 and other types.

For example, I see both:

string text = "Some Text Here";

String text = "Some More Text Here";

What is the difference between them?

Short Answer

None. There is no difference between them. Use the one you like best.

Really? No difference whatsoever?
Why have both forms then? What’s the point?


Continue reading “C#: string or String?”

SQL Trace And The Stream Of Query Plans

In 8 Ways To Hunt T-SQL Query Plans, we looked at some simple ways to get our hands on those valuable query plans. However, sometimes we might want to record query plans as part of a bigger effort to profile and troubleshoot a server, or perhaps optimize it for a given workload.

When this need arises, there are two additional tools we can make use of:

  • SQL Trace
  • Extended Events

In this pumpkin we’ll take a look at how we can use SQL Trace to accomplish this goal.

Continue reading “SQL Trace And The Stream Of Query Plans”

How To Edit The Windows Registry In PowerShell


How can I access the Windows Registry in PowerShell so I can create, update and delete keys and values?

Short Answer

Use the Get-ItemProperty and Set-ItemProperty Cmdlets:

# get the data from a specific registry key value
Get-ItemProperty "HKLM:\SOFTWARE\MyRegistryKey" |
    % { $_.MyKeyValue };

# set the data for a specific registry key value
Set-ItemProperty "HKLM:\SOFTWARE\MyRegistryKey" -Name "MyKeyValue" -Value "Some Data";

Long Answer

When playing with the Windows Registry, the common Item Cmdlets are our friends:

Continue reading “How To Edit The Windows Registry In PowerShell”

How To Gracefully Stop An Asynchronous Job In PowerShell


How can we ask an asynchronous PowerShell job to gracefully stop on its own?

Short Answer

We can use a marker file pattern for that effect:

# this is the path to the marker file
$JobStopMarkerFile = "C:\Temp\JobStopMarker.txt";

# ensure the marker file does not exist
if (Test-Path -LiteralPath $JobStopMarkerFile)
    Remove-Item $JobStopMarkerFile

# this contains the script to run async
$JobScript = {

        [Parameter(Mandatory = $true)]

    # this flag will be set when the job is done
    $IsJobDone = $false

    # this condition checks whether
    # 1) the job is done
    # 2) the job has been asked to stop
    while (-not $IsJobDone)
        # some recurring task would run here instead
        Start-Sleep -Seconds 5

        # uncomment and set this whenever the job finishes on its own
        # $IsJobDone = $true

        # check if the marker file exists
        if (Test-Path -LiteralPath $JobStopMarkerFile)
            # signal the job as completed
            $IsJobDone = $true;

            # cleanup the file too
            Remove-Item -LiteralPath $JobStopMarkerFile

# start the job now
$Job = Start-Job -ScriptBlock $JobScript -ArgumentList $JobStopMarkerFile

# do some other stuff here
Start-Sleep 1

# ask the job to stop
# we do this by *creating* an empty marker file
# the job itself will remove this file
$null > $JobStopMarkerFile

# wait for the job to finish
Wait-Job $Job

There is quite some code here, isn’t there? So how does it work?

Long Answer

If we just want to stop a job, minus the gracefully bit, we can simply use the Stop-job Cmdlet:

Continue reading “How To Gracefully Stop An Asynchronous Job In PowerShell”

How To Discover The Installed Version Of PowerShell


How can we discover which version of PowerShell we are running on?

Quick Answer

We can use the $PSVersionTable system variable:


This will return an object with the installed version build numbers:

PowerShell Installed Version

Long Answer

We can use $PSVersionTable to discover the versions of different components of PowerShell:

Continue reading “How To Discover The Installed Version Of PowerShell”

How To Uninstall An Application In PowerShell


How can we uninstall an application listed in Programs and Features using PowerShell?

Short Answer

We can use the Win32_Product WMI class.

$app = Get-WmiObject
    -Class Win32_Product
    -Filter "Name = 'Application Name'"

Long Answer

We can use the Win32_Product WMI class to query the system for installed applications.

Continue reading “How To Uninstall An Application In PowerShell”

8 Ways To Hunt T-SQL Query Plans

In How Can Query Plans Help Us? we looked at how query plans can be made to help us troubleshoot a runaway query. Then in Are Query Plans All We Need? we looked at other tools we need to keep ready in our performance tuning utility belt. With that in mind, it is now time to understand how we can capture query plans to begin with.

So Where Do We Start?

To understand how we can capture query plans, we must first understand what types of query plans are there to capture. Luckily, there is only two of them.

Continue reading “8 Ways To Hunt T-SQL Query Plans”