All posts in PowerShell

Add SIP Address to Match Primary SMTP

Hi All

Here is a little snippet to add a SIP address to a users mailbox so that it matches the users PrimarySmtpAddress.

Change the $mbxes variable to suit your needs and run.

Worked like a charm :-)

Purge a Directory Synchronised Office 365 Account and Resynchronise without Removing from AD

To purge and resynchronise an AD synchronised with DirSync account from Office 365, follow these steps:

1.    Fire up Windows Azure Active Directory for Windows PowerShell (Check out managing Office 365 through Azure PowerShell here).

2.    Run the following command to connect to online service, remembering substitute our friend Fakey McFakerson for your own admin credentials.


3.    Now, we need to grab the user Object ID. Once you obtained it copy and paste it elsewhere as you’ll need it later.


4.    Ok, now that we have the user Object ID we can remove the account.


5.    With the account removed, we now need to purge the account from the Office 365 Recycle Bin. This is where we use the Object ID obtained earlier. Run the following command to purge the user from recycle bin.



With the user(s) accounts purged from Office 365 you need to perform the Directory Synchronisation (unless you are happy to wait for it to sync on the next run? Default is every three hours).

6.   To force immediate directory synchronization, type the following into a run window on the Directory Synchronisation server:

C:Program FilesMicrosoft Online Directory SyncDirSyncConfigShell.psc1

This opens up a PowerShell window with the appropriate synchronisation commands pre-loaded.

7.   Type the following at the command prompt –


And that is it my friends, relatively quick and painless compared to the alternative of having to delete and recover an AD account or recreate from scratch. Remember to leave a comment or a like if this helps you out. Here is the code in a single block for you to copy and paste with greater ease…





Deleting Folders and Files with PowerShell

Ok, so you are running out of space on that drive and need to clean up. Of course you could jump into the GUI and manually select the files and folders but that is just so labour intensive. Fire up PowerShell and I’ll walk you through the Remove-Item cmdlet. Check out the TechNet article here:

The Remove-Item cmdlet does exactly what it says on the tin. It deletes one or more items and because it is supported by many providers, it can delete many different types of items, including files, directories, registry keys, variables, aliases, and functions.

Use “Get-Help Remove-Item –Full” for a full description of the cmdlet and the associated switches. You’ll notice that there are a number of Aliases associated with the cmdlet that we can utilise to save our fingers. If you just want some examples to show you what to do use “Get-Help Remove-Item –Examples”.


ri, rm, rmdir, del, erase, rd

So, instead of typing “Remove-Item deleteMe.txt” to delete the text file “deleteMe.txt” in the current working directory, we can just use one of the aliases and shorten the command to “ri deleteMe.txt”. Cool, huh. Use which ever alias makes sense to you. I tend to use “del” personally…

Ok, let’s have a look at some examples.

EXAMPLE 1: Remove all files from C:DeleteMeFolder. It is important to note that the use of the period (.) will not delete directories or files with no extension.


EXAMPLE 2: Remove all word documents from the current directory BUT not any documents with “keepMe” in the filename that does not include “1”. The current directory is specified with the use of the wildcard and the command utilises the Include and Exclude parameters to specify the files to delete.


EXAMPLE 3: Remove a hidden and/or read only file from the path C:DeleteMeFolder, note the use of the Force parameter that forces permission.


EXAMPLE 4: This command deletes all of the CSV files in the current directory and all subdirectories recursively. Because the Recurse parameter in the Remove-Item cmdlet has a known issue (it might not delete all child directories or files, especially if the ‘Include’ parameter is added to the command), the command in this example uses the Get-ChildItem cmdlet to get the desired files, and then uses the pipeline operator to pass them to the Remove-Item cmdlet. In the Get-ChildItem command, the Path parameter has a value of *, which represents the contents of the current directory. It uses the Include parameter to specify the CSV file type, and it uses the Recurse parameter to make the retrieval recursive. If you try to specify the file type in the path, such as “-path *.csv”, the cmdlet interprets the subject of the search to be a file that has no child items, and Recurse fails.


EXAMPLE 5: We can even remove an old registry item! This command deletes the OldApp registry key and all of its subkeys and values. It uses the Remove-Item cmdlet to remove the key. The path is specified, but the optional parameter name (Path) is omitted. The Recurse parameter deletes all of the contents of the OldApp key recursively. If the key contains subkeys and you omit the Recurse parameter, you are prompted to confirm that you want to delete the contents of the key.


EXAMPLE 6: And finally, sometimes permission errors on files or subfolders can cause the command to fail. Normally, you would sigh and painfully go through each folder manually in the GUI after taking ownership. However, by defining an ErrorAction we can simply keep going. Here we completely delete a folder along with files and sub-folders in it and force it to continue.


I hope you find this useful and remember you are deleting stuff here. Be careful. In fact, you can perform a test run of your commands by using the -WhatIf variable. Try it out by simply adding -WhatIf to the end of any of the above examples and PowerShell will output the results of a dry run. In other words it will analyse and produce expected results without performing the action. Very, very handy variable.