Powershell Function Return Not What I Expected

October 24th, 2010 by Mitchell Lintzen

I had a Powershell Function that was supposed to return an Integer data type but occasionally would return an array or “system.object” type. I was checking the value with a $var.GetType().Fullname just before the “return” in the function and then after in the main body of the script and I could see that the value and the type of the variable was being changed. I finally found this article that explained output in Powershell:


I had a “$string -match” inside of the function. That returns a True/False and then creates and array of the regular expression matches ($matches[x]). So every time I would see something like “True 58” as the return instead of just “58”. Then I found this article that helped to pipe the Boolean return of –match to $null:


I’ve had to use the redirect to null before but it’s been a long time. It all clicked once I understood how Powershell returns data from functions.

Gather ActiveSync Device Information From Exchange 2007

October 21st, 2009 by Mitchell Lintzen

To gather a complete list of mobile devices that are connecting to your Exchange 2007 Client Access Servers we need to retrieve the full list of mailboxes and then pipe that to the Get-ActiveSyncDeviceStatistics commmand as such:

$mob_devices = get-mailbox -ResultSize unlimited |%{ Get-ActiveSyncDeviceStatistics -mailbox $_}

That command will take a long time depending on how large your organization is. Then we’ll need to iterate through each device and weed out those devices that are no longer attempting to sync with Exchange. I’ve found that looking back 14 days should be enough time to eliminate the stale partnerships.

foreach($device in $mob_devices){
if((get-date).adddays(-14) -le $device.lastsyncattempttime){
$smtp = $device.identity.smtpaddress
$name = (get-mailbox $smtp).name
$type = $device.devicetype
“$smtp    $name    $type” | out-file -append ./mob_devices.txt

The last line outputs the SMTP address, user name, and device type to a text file. Have fun!

Exchange 2007 Powershell Identity Management Automated Mailbox Provisioning Script

October 18th, 2009 by Mitchell Lintzen


Exchange 2007 Powershell Automated Distributrition List Synchronization Script

October 18th, 2009 by Mitchell Lintzen

Create & Synchronize Membership with Active Directory OU – Containers

Exchange 2007 Powershell Mailbox Migration Script

October 17th, 2009 by Mitchell Lintzen

For Single Geographical Server(s) Location Microsoft Exchange 2007 Topology

Database IO & Storage CapacityLoad Balancing

Alphabetic Database Naming Scheme

Based On Statistical Analysis Of Last Name Distribution Within Active Directory Mail-Enabled Accounts

iTunes Management With Powershell

October 7th, 2009 by Mitchell Lintzen

I recently converted from Windows Mobile to iPhone and I needed to figure out a way to manage my mobile MP3 library. With Windows Mobile I used Powershell and accessed the file system on my Windows Mobile device as a USB flash drive. To manipulate playlists on the iPhone I tried a few applications such as Winamp and MediaMonkey but they would not let me apply the time & genre based logic that I needed. So I found iTunesLibraryEditor which allowed me to access and modify iTunes playlists by instantiating the iTunesLibraryEditor object as such:

$lib = new-object ituneslibraryeditor.ituneslibrary

Check out the code in my Powershell script library: BinaryAgent Powershell Repository. Look for “Sync-iPhone.zip” in the /Sync-iPhone/ directory. Hope you find it useful.