Calculating Standard Deviation using PowerShell

I have been reading a book on data science recently, and as a result I have had to revisit my high school maths!
One interesting thing I found is that there is more than one way to calculate the end result, and that the standard taught method may not be the most accurate!
The most accurate (according to the experts) is a method created by B.P.Welford, which is detailed in-depth in Donald Knuth’s ‘Art of computer programming’.

My brain thought “I wonder if anyone has implemented this in PowerShell”?

I found it in many difference languages, but not PowerShell, so I thought I would take someone else’s hard work and translate!

The site I choose is

My version does not yet work as a class where you may ‘pop’ on another value dynamically, it assumes instead that you have the figures, and you would like to work out the Standard Deviation using either a Sample or a Population variance.

The code is as follows:

function Get-StandardDeviation {
    Param (
    # Array of double values
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    Begin {

    Process {
        foreach ($value in $values) {
            $delta = $mean + (($value - $mean) / $count)
            $sum += ($value - $mean) * ($value - $delta)
            $mean = $delta
    } # process

    End {
        $VariancePopulation = $sum/($count)
        $VarianceSample = $sum/($count-1)
            "VariancePopulation" = $VariancePopulation
            "VarianceSample" = $VarianceSample
            "STDEVPopulation" = [Math]::Sqrt($VariancePopulation)
            "STDEVSample" = [Math]::Sqrt($VarianceSample)
            "Mean" = $mean
            "Count" = $count
        Write-Output $obj
    } #end


To test this, create an array of doubles, and then use either method shown below to get the results.

$data = (50.0, 45.0, 55.0, 58.0, 43.0, 49.0, 50.0)</code>

Get-StandardDeviation -Values $data
$data | Get-StandardDeviation


Error: The Microsoft Online Services Module is not configured properly

What I was trying to achieve

I needed to use the MSOnline PowerShell Module to restore a deleted user from the Azure Active Directory (AAD) Recycle Bin. The replacement Module AzureAD does not (to my knowledge) have this functionality.

The Error

When trying to connect to AAD using the Connect-MsolService command, I received the following error:

Connect-MsolService : The Microsoft Online Services Module is not configured properly. Please uninstall and then reinstall the module.

I took the error messages advice, but to no avail.


The solution is in the registry…

Create a .reg file with the following content:

Windows Registry Editor Version 5.00

"InstallPath"="c:\\Program Files\\WindowsPowerShell\\Modules\\MSOnline\\1.0\\"


Import the reg file, then retry the command.

Thank you to user ‘Froggy’ who commented the solution at the following URL:



Connecting Virtual Networks in different Azure Subscriptions to an ExpressRoute circuit in Resource Manager


I have an ExpressRoute circuit configured with a connection to a Version 2 (Resource Manager) Azure Virtual Network. I also have an additional Virtual Network in a different Subscription which I need to connect to the same circuit.

The Issue

The instructions on connecting another Virtual Network in a different Subscription are a little confusing. It’s also worth noting that some of the parameters are different now with the latest version of the PowerShell Azure cmdlets. The original instructions may be found at this URL:

Continue reading

Replacing GUIDS in a file using PowerShell

A quick line of PowerShell code that replaces existing GUIDS in a file with completely new randomly generated GUIDS (Should all be on one line):

(get-content .\Input.xml) | 
ForEach-Object {$_ -replace "\{[0-9a-z\-]*\}",
("{" + [guid]::newguid()  + "}")} |
 Set-Content output.xml -Encoding UTF8

The code basically does a find and replace using a regular expression, then outputs the new content to the output.xml file. The encoding output is completely up to you, mine is set to UTF-8 as it matched the input file.