Determine if windows is currently playing sound

Here is a sample C# code that determines if Windows is rendering any audio stream. It uses Windows Core Audio API (specifically the IAudioMeterInformation interface) and is supported on Vista and higher. public static bool IsWindowsPlayingSound() { var enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); var speakers = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); var meter = (IAudioMeterInformation)speakers.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero); var value … Read more

Calling generic static method in PowerShell

The easiest way to call MyMethod is, as @Athari says, to use MakeGenericMethod. Since he doesn’t actually show how to do that, here is a verified working code sample: $obj = New-Object Sample $obj.GetType().GetMethod(“MyMethod”).MakeGenericMethod([String]).Invoke($obj, “Test Message”) $obj.GetType().GetMethod(“MyMethod”).MakeGenericMethod([Double]).Invoke($obj, “Test Message”) with output Generic type is System.String with argument Test Message Generic type is System.Double with argument … Read more

How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell?

You can do this using Select-String in PowerShell 2.0 like so: Select-String F\d\d -input $string -AllMatches | Foreach {$_.matches} A while back I had asked for a -matchall operator on MS Connect and this suggestion was closed as fixed with this comment: “This is fixed with -allmatches parameter for select-string.”

Hashtables and key order

There is no built-in solution in PowerShell V1 / V2. You will want to use the .NET System.Collections.Specialized.OrderedDictionary: $order = New-Object System.Collections.Specialized.OrderedDictionary $order.Add(“Switzerland”, “Bern”) $order.Add(“Spain”, “Madrid”) $order.Add(“Italy”, “Rome”) $order.Add(“Germany”, “Berlin”) PS> $order Name Value —- —– Switzerland Bern Spain Madrid Italy Rome Germany Berlin In PowerShell V3 you can cast to [ordered]: PS> [ordered]@{“Switzerland”=”Bern”; “Spain”=”Madrid”; … Read more

PowerShell script to check the status of a URL

I recently set up a script that does this. As David Brabant pointed out, you can use the System.Net.WebRequest class to do an HTTP request. To check whether it is operational, you should use the following example code: # First we create the request. $HTTP_Request = [System.Net.WebRequest]::Create(‘http://google.com’) # We then get a response from the … Read more