Category Archives: Powershell

RunProcess from Powershell

Sometimes I want to run older programs / tools like MSIEXEC or such to run from within my Powershell scripts or modules. I wanted to make it easy for myself to format the exact command line, and capturing non-standard output that is written to the screen .

I wrote this simple function to start any  .exe or other w32 process, wait for it to complete, and capturing both the exit code and the full output in an array while the window remains hidden.

Example call to this function:

$res = runProcess msiexec "/i AcroRdrDC1500720033_nl_NL.msi /quiet"
write-host "Result code of msiexec: $($res[0])"
write-host "All output of msiexec: $($res[1])"

And here is the function:

function runProcess ($cmd, $params, $windowStyle=1) {
$p = new-object System.Diagnostics.Process
$p.StartInfo = new-object System.Diagnostics.ProcessStartInfo
$exitcode = $false
$p.StartInfo.FileName = $cmd
$p.StartInfo.Arguments = $params
$p.StartInfo.UseShellExecute = $False
$p.StartInfo.RedirectStandardError = $True
$p.StartInfo.RedirectStandardOutput = $True
$p.StartInfo.WindowStyle = $windowStyle; #1 = hidden, 2 =maximized, 3=minimized, 4=normal
$null = $p.Start()
$output = $p.StandardOutput.ReadToEnd()
$exitcode = $p.ExitCode

AutoBuilder, a self restarting Powershell script for orchestration

I was recently asked to build a Powershell script to fully automate building up a server, including detailed configuration and installation of roles and other software.

Some of these actions required a reboot. After a reboot, the Powershell script had to restart itself. When running a script remotely, Workflows can be used, but when running the script locally I could not get this to work properly. Thus, I built a self-restarting script template that will run each phase as you configure it.

This self-resuming Powershell script writes its own run configuration to a Scheduled task that will run at boot time without user interaction, under the SYSTEM account. It will run all the commands you specify, reboot and resume when necessary, and unregister itself when it has completed.

This is awesome for, for example, building up Terminal Servers or Web Servers in a highly virtualized environment. Of course there are many orchestration tools available, and they may be better suited for this task, but those were not available when I was asked to code this.

Download it here: AutoBuilder_v0.3

Or check out the source code:

Continue reading AutoBuilder, a self restarting Powershell script for orchestration


Slowly but surely, the Office 365 dev team is adding reporting functionality to their platform, to the delight of admins and managers alike. For admins it means a lot less scripts to write, for managers it means knowing….stuff.

One report I missed was a report that tells me when users last logged on. Because if I have thousands of users, and they all consume licenses….I’d very much like to strip licenses from users that haven’t logged in since x amount of time.

Especially for companies with geographically dispersed users and inefficient exit procedures, this can save a lot of licensing costs over time.

My report was built in Powershell, and will check the last time the mailbox was accessed to determine the last logon date, this is not perfect, as I can image some organisations use specific licenses just for skype or dynamics, they will not benefit as much from this script, but in 99% of the times it should suffice 🙂

The script will list the user UPN, Name, Last Logon, Creation Date, Usage Location, Mailbox Size and Used Licenses.

Download: LicReport365_v0.5


Continue reading LicReport365

Get last logon times for all Exchange Online users

If you want to figure out when your users last logged on, perhaps to clean up licenses in use by dormant accounts, the following Powershell code may help you.

#Copyright: Free to use, please leave this header intact
#Author: Jos Lieben (OGD)
#Company: OGD (
#Purpose: Generate a CSV file with last logon times of all Office 365 users

$csv = "c:\temp\LastLogons_$(Get-Date -format dd_MM_yyyy).csv"
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$users = get-mailbox -ResultSize Unlimited | select UserPrincipalName
Foreach ($user in $users){
$mbx = get-mailboxstatistics -Identity $($user.UserPrincipalName) | Select LastLogonTime
$upn = $user.UserPrincipalName
if ($mbx.LastLogonTime -eq $null){
$res = "Never"
$res = $mbx.LastLogonTime
$outStr = "$upn,$res"
Out-File -FilePath $csv -InputObject $outStr -Encoding UTF8 -append

Setting administrative permissions on all your Onedrive for Business accounts

Managing permissions on your user’s Onedrive for Business storage is a chore, there is no direct interface to do this in bulk, nor is the interface very easy to find. Plenty of articles explain how to do this for ONE user through the GUI, but few explain how to do this in bulk for several users at once.

And when you’re migrating, for example, hundreds or thousands of homedirectories to Onedrive For Business, you’ll want to automate setting permissions on all these users in bulk.

Fortunately, this can be scripted using Powershell, probably after you’ve bulk-provisioned your users in Continue reading Setting administrative permissions on all your Onedrive for Business accounts