A PowerShell module for managing your Gmail, with all the tools you'll need. Search, read and send emails, archive, mark as read/unread, delete emails, and manage labels.
This library is still under development.
You can install it easily using chocolatey:
cinst Gmail.ps
Or install it manually:
git clone https://github.com/nikoblag/Gmail.ps.git
cd Gmail.ps
.\install.ps1
- Read emails
- Search emails
- (Update) emails: label, archive, delete, mark as read/unread/spam, star
- Manage labels
- Move between labels/mailboxes
- Automatic authentication, using the Windows Credential Manager
To authenticate a Gmail session, use New-GmailSession
and provide your username and password.
If you want to be automatically logged in to your account, create a generic credential using the Windows Credential Manager:
go to 'Control Panel\User Accounts and Family Safety\Credential Manager', click 'Add a generic credential', then type your
Gmail username and password, and use Gmail.ps:default
as address.
PS> $gmail = New-GmailSession
PS> # play with your gmail...
PS> $gmail | Remove-GmailSession
If you use Invoke-GmailSession
and pass a block, the session will be passed into the block,
and will be logged out after it's executed. Use $args
to access the session or parameterize the block:
PS> Invoke-GmailSession -ScriptBlock {
PS> param($gmail) # to use $gmail instead of $args
PS> $gmail | Get-Label
PS> }
You can also check which accounts are logged in at any time:
PS> Get-GmailSession
Get the messages in the inbox:
PS> $gmail | Get-Mailbox
PS> $gmail | Get-Mailbox | Filter-Message -Unread
Get the messages marked as Important by Gmail:
PS> $gmail | Get-Mailbox "Important"
With Get-Mailbox
you can access the "All Mail"
, "Starred
", "Drafts"
, "Important"
, "Sent Mail"
and "Spam"
folders
Filter with some criteria:
PS> $gmail | Get-Mailbox | Filter-Message -After "2011-06-01" -Before "2012-01-01"
PS> $gmail | Get-Mailbox | Filter-Message -On "2011-06-01"
PS> $gmail | Get-Mailbox | Filter-Message -From "x@gmail.com"
PS> $gmail | Get-Mailbox | Filter-Message -To "y@gmail.com"
Combine flags and options:
PS> $gmail | Get-Mailbox | Filter-Message -Unread -From "myboss@gmail.com"
Browsing labeled emails is similar to working with the inbox.
PS> $gmail | Get-Mailbox -Label "Important"
You can count the messages too:
PS> $gmail | Get-Mailbox | Filter-Message -Unread | Count-Message
PS> $gmail | Count-Message
Also you can manipulate each message using block style. Remember that every message in a conversation/thread will come as a separate message.
PS> $messages = $gmail | Get-Mailbox | Filter-Message -Unread | Select-Object -Last 10
PS> foreach ($msg in $messages) {
>> $msg | Update-Message -Read # you can use -Unread, -Spam, -Star, -Unstar, -Archive too
>> }
Delete emails from X:
PS> $gmail | Get-Mailbox | Filter-Message -From "x@gmail.com" | ForEach-Object { Remove-Message $_ }
Save all attachments in the "Important" label to a local folder:
PS> $messages = $gmail | Get-Mailbox -Label "Important" | Get-Message
PS> foreach ($msg in $messages) {
>> if ($msg.HasAttachments) {
>> $msg.FetchAttachments($folder)
>> }
>> }
Save just the first attachment from the newest unread email:
PS> $msg = $gmail | Get-Mailbox | Filter-Message -Unread | Select-Object -Last 1
PS> $msg.Fetch()
PS> $msg.Attachments[0].SaveTo($location)
Get all labels applied to a message:
$msg | Get-Label
Add a label to a message (or remove it):
$msg | Set-Label "Important"
$msg | Remove-Label "Important"
You can apply multiple lables:
$msg | Set-Label "Important","Banking"
The example above will raise error when you don't have one of the specified labels. You can avoid this using:
$msg | Set-Label "Important","Banking" -Force # If one of the labels does't exist, it will be automatically created now
You can also move message to a label/mailbox:
$msg | Move-Message -Label "Test"
$msg | Move-Message "All Mail"
With the Gmail module you can also manage your labels. You can get list of defined labels:
$gmail | Get-Label
Create new label:
$gmail | New-Label -Name "MyLabel"
Remove labels:
$gmail | Remove-Label -Name "MyLabel"
Or check if given label exists:
$gmail | Get-Label -Name "SomeLabel" # returns null if the label doesn't exist
- Write tests
- Prettify the output
- Send mail via Google's SMTP servers
- Fork it.
- Create a branch (
git checkout -b my_feature
) - Commit your changes (
git commit -am "Added Feature"
) - Push to the branch (
git push origin my_feature
) - Open a Pull Request
- Enjoy an ice cream and wait
- Nikolay Blagoev [https://github.com/nikoblag]
- Copyright (c) 2013 Nikolay Blagoev
- Copyright (c) 2013 Andy Edinborough - AE.Net.Mail library
- Copyright (c) 2012 Tobias Burger - Get-StoredCredential cmdlet
See LICENSE for details.