Friday, 12 September 2014

Advanced Search Query

Use the Search-Mailbox cmdlet for the following scenarios

1. Deleting the meetings from the end user calendar.

2. Delete the Mail from the end user Mailbox

3. Copy the mails from one mailbox to other mailbox

4. Delete the meetings made by terminated users

5. Delete the meeting from Resource mailbox

Use the below cmdlet for taking the backup and see what emails you are going to recall. So if anything goes wrong you could use this backed up emails. 

search-mailbox -searchquery "kind:meetings from:$recipient" -targetmailbox $yourmailid -targetfolder "REPORT" 

Check $yourmailid to see what meetings you are going to delete. If you are ok then go ahead and use delete parameter for deleting the meeting

search-mailbox -searchquery "kind:meetings from:$recipient"  -deletecontent -force

Use the below cmdlet for copying emails from one mailbox to another mailbox

Search-Mailbox -SearchQuery "Kind:email AND sent: 06/01/14..09/16/14" -TargetMailbox  -TargetFolder Backup

Monday, 7 July 2014

Powershell Error Handling

There are two common parameters for error handling.

-Erroraction and -Errorvariable

To know about common parameters:

Get-Help -Name about_CommonParameters;

if an error occurs while running the script it will save that error in automatic variable

Automatic variable : $error

Error handling is very important when you write scripts. You should know what is happening when the script is running. you could catch errors if the error occurs while running the script









Put your scripts in Try block and Catch block would catch the errors while running the scripts.

Monday, 23 June 2014

Remote Powershell

Use remote powershell if you dont have installed Exchange Management tool.

Use the below script to log in through remote powershell

$username = 'domain\username'
$password = '**************'
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

$session = New-PSSession -Authentication basic -Credential $cred -ConnectionUri https://*****.******.com/PowerShell -ConfigurationName Microsoft.Exchange
Import-PSSession $session

If you want to login to the different machine through remote use the below code

$session = New-PSSession -Credential $cred -ComputerName <remote machine>
Enter-PSSession $sessio

To exit the remote session

Remove-PSSession $session

Powershell Execution policy

if the powershell scripts to be allowed to run the scripts on your computer, Use the below execution policy cmdlets

Set-ExecutionPolicy RemoteSigned

Then use the below cmdlet to check the execution policy


Friday, 9 May 2014

Convert Powershell Script as Executable File

How to convert the Powershell Script as Executable File?

Sometimes we need to run the PS script as an executable file. In this case we could convert the powershell script as an exe file. Then run it on the windows OS. Please pay the attention on the Microsoft .Net Framework while creating the executable file.

1. Install the latest version of PowerGUI Script Editor

2. Write the script on the PowerGUI Script Editor which you want to convert it as EXE file

3. Then Click on "Tools"

4. Then Click on "Compile Script"

5. If you want you can show powershell script on console. Else you could disable that option

Wednesday, 22 January 2014

Disk Usage Information

Program to get Disk usage information 

Use the below script to find the disk usage on windows. This script would be helpful when you want to work on disk utilization tasks such as cleanup process on  disks, Auditing purpose etc,.

Use this script to check the limit of disk utilization. And use Send-Mailmessage to send the alert to the specific person and schedule the script on task scheduler.

function get-DiskUsage {

Get-ChildItem -Recurse -Directory | Select-Object FullName,
@{ Name="Size";
Expression={ ($_ | Get-ChildItem |
Measure-Object -Sum Length).Sum + 0 } }

Thursday, 9 January 2014

Make your Computer to speak with Powershell

Use the COM object to make the computer to speak. We could intimate the administrator using this computer speaking feature using powershell when they receive an error. This would be helpful to the scripting developer.

Use the New-Object cmdlet to create the COM object as mentioned in the below screenshot.

.Net Static Method

How to list the static method using Get-Member cmdlet?

Once you know the .net type, we can list the static method using the Get-Member.


[Sytem.Guid] -> .Net Type

Newguid() -> .Net Method

Have look at the below screenshot.

If you are in a situation to fetch the local machine timezone, you can use the method as mentioned in the below screenshot.