Query Plans In The Race Of Extended Events

In the previous articles we took a look at some simple ways to hunt for those valuable query plans and then at how we can accomplish the same using SQL Trace. There is yet another way of discovering and extracting query plans from an in-use SQL Server and that is to use Extended Events.

Continue reading “Query Plans In The Race Of Extended Events”

How To Join Object Lists In PowerShell


So I have these two arrays of data objects in my PowerShell code:

$Customers = (
    @{ Id = 1; Name = "Customer 1" },
    @{ Id = 2; Name = "Customer 2"; },
    @{ Id = 3; Name = "Customer 3"; }

$Orders = (

    @{ Id = 1; CustomerId = 1; Product = "Red Pumpkin" },
    @{ Id = 2; CustomerId = 1; Product = "Blue Pumpkin" },
    @{ Id = 3; CustomerId = 2; Product = "Green Pumpkin" }

How can I mimic a SQL-like JOIN on these two lists and output a new list with the joined data?

Short Answer

Looping and New-Object are your friends:

# loop all customers
foreach ($c in $Customers)
    # loop al orders
    foreach ($o in $Orders)
        # decide whether to join
        if ($o.CustomerId -eq $c.Id)
            # output a new object with the join result
            New-Object PSObject -Property @{
                CustomerId = $c.Id;
                OrderId = $o.Id;
                CustomerName = $c.Name;
                OrderProduct = $o.Product;

What would be really cool though would be to have a CmdLet that would do this work for us dynamically whilst allowing us to pipe data through, wouldn’t it?

Continue reading “How To Join Object Lists In PowerShell”

How To Access .NET Property Types Dynamically In PowerShell


So I have this given list of objects. I don’t know their structure beforehand. I want to be able to access their property values and underlying data types to accomplish some other task.

Here is some dummy data as an example:

$DataList = (
    @{ Id = 1; Name = "Red Pumpkin" },
    @{ Id = 2; Name = "Green Pumpkin" },
    @{ Id = 3; Name = "Blue Pumpkin" }
) | %{ New-Object PSObject -Property $_ };

How do I do this?

Continue reading “How To Access .NET Property Types Dynamically In PowerShell”

How To Find And Remove Old Files In PowerShell


So I have all these files in my backup folder. How can I keep only the files modified at most seven days ago, whilst removing the rest?

Short Answer

We can use a simple chain like this:

Get-ChildItem -Recurse |

    # change the extension as appropriate
    ?{ $_.Extension -eq ".bak" } |

    # change the time window as appropriate
    ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-7) } |

    # now remove the results

We can then add or remove filters to this pattern as appropriate to your scenario.

So what more stuff can we add?

Continue reading “How To Find And Remove Old Files In PowerShell”

C#: Why Are My Random Numbers Not Random?


So I have this code, which is supposed to generate a new random number every time it is called:

        public static int GetSomeRandomNumber(int min, int max)
            Random random = new Random();
            return random.Next(min, max);

In my program, I’m using the above method to let me select five pumpkin colors at random (just don’t ask why):

        enum PumpkinColor
            Red = 0,
            Green = 1,
            Blue = 2

        static void Main(string[] args)
            // write five random pumpkin colors
            for (int i = 0; i < 5; ++i)
                PumpkinColor color = (PumpkinColor)GetSomeRandomNumber(0, 3);

However, when I run my program, this is the output:


Blimey. Not very random now, is it? What gives?

Short Answer

Don’t use a local Random instance in quick loops. Use a shared instance instead:

        private static Random random = new Random();

        public static int GetSomeRandomNumber(int min, int max)
            return random.Next(min, max);

This new implementation now returns:


But why does this happen?
And what care do I need to have with static implementations?

Continue reading “C#: Why Are My Random Numbers Not Random?”

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”