Deleting an email using powershell - powershell

I want to delete an email from Exchange serveur and then from the mailbox received items,so I used this following powershell command but after a while , powershell_ise crashes and it closes
Search-Mailbox -Identity "Khalil Med" -SearchQuery 'Subject:"Phishing"' -DeleteContent

I live in the ISE daily and sometimes VSCode as needed and I do this sort of thing regularly in the ISE and VSCode, neither have crashed on me for this kind of use case. Yet, environments can differ. I find it hard to understand how running this simple request would crash the ISE, as it's not doing anything $psISE / ISE GUI specific.
Are you doing this on the Exchange server directly, or via Explicit or Implicit remote session from your admin workstation?
Also, make sure you have the RBAC role to perform this...
Get-ManagementRoleAssignment -Role 'Mailbox Import Export'
New-ManagementRoleAssignment -Role 'Mailbox Import Export' -User 'Administrator'
... and make sure you are getting something to act on, meaning actual emails using...
Search-Mailbox -Identity 'HostMaster TestUser' -SearchQuery "Subject:test" -EstimateResultOnly
If the response from the above is '0', then well, you now know why the -DeleteContent did not actually work.
As a sanity check. Rather than looking at one account. Look at all accounts to see if you are getting any hits on anyone..
Get-Mailbox -ResultSize Unlimited |
Search-Mailbox -SearchQuery 'Subject:test' -EstimateResultOnly -Force |
Where-Object -Property ResultItemsCount -gt 0

Related

Why does Enable-Mailbox show all mailbox properties when finished?

I have a Powershell script that creates AD Users from a CSV file and creates a mailbox in our on-premises Exchange server. The script works well, but every time it finishes the 'Enable-Mailbox' command, it outputs the mailbox properties as if 'Get-Mailbox | FL' is being called. The command is used as follows:
Enable-Mailbox -Identity <identity> -Alias <alias> -Database <database>
This link says that Enable-Mailbox has no output (https://learn.microsoft.com/en-us/exchange/client-developer/management/exchange-management-shell-cmdlet-input-and-output-types), and this command is at the end of the loop, so nothing else would be outputting anything before moving on to the next user.
Can anyone shed some light on what's going on or point me in the direction to figure out why this is happening?
EDIT: I'm piping to Out-Null to suppress the output for now

Using Read-Host to output information from a script

I am trying to run a script against exchange to bring back all of the mailboxes a certain user has access to. I want to be able to input the usersname using read-host. I currently have this:
$username = Read-Host("Please enter users username")
#Enable Exchange cmdlets
add-pssnapin *exchange* -erroraction SilentlyContinue
Get-MailBox | Get-MailboxPermission -User $username | FL > C:\MailboxPermissions.txt
However, when I run this via powershell, it asks for the username, looks like it is starting to run the script, then powershell just exits and there is not data outputted
Any help would be greatly appreciated
Thanks for all the help
I finally figured it out and there were a couple of issues. It was to do with the result size. I added -resultsize unlimited:
$username = Read-Host("Please enter users username")
add-pssnapin *exchange* -erroraction SilentlyContinue
>Get-MailBox -resultsize unlimited | Get-MailboxPermission -User $username | FL > C:\MailboxPermissions.txt
It would also not work by running the .ps1 file as this was not run by admin, and it needs admin permissions to output to the location I want. Once I created a shortcut for it to run via the powershell.exe with admin credentials it is now working as expected.
The problem is that you are only out putting to the screen.
This means that when you run your script it will carry out the required action, print to screen and close the window immidiatly. In turn, this means you can't see the output.
As #DarkLite1 mentioned, you could output to a file.
Or, you could simply allow the console to wait before closing. This is done like this at the end of your code:
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
You may also need a Write-Host on the last action in your code snippet, I'm not entirely sure as I am not familiar with how Get-Mailbox works, but try it without first.
To summarize, You must keep the window open or print the results to file to actually see anything. The code you have currently will complete so fast you will never see any output.

Get-inboxrule -mailbox jdoe | disable-inboxrule -confirm:$false >>>> not working.

I have a powershell script that disable user's AD accounts. One part is to disable the exchange inbox rules.
However when I try to run this.
Get-inboxrule -mailbox jdoe | disable-inboxrule -confirm:$false
I get this confirm popup.
Using Outlook Web App or Windows powershell to modify your rules will delete any rules what were previously turned off using outlook. If you want to preserve the rules you turned off using Outlook, select No and user outlook to edit your rules, are you sure you want to proceed?
How can I stop the confirm prompt?
think this did it.
Get-inboxrule -mailbox jdoe | disable-inboxrule -confirm:$false -AlwaysDeleteOutlookRulesBlob

Powershell: Re-create RDS Remote Apps by Looping?

I'm stumped. I can usually take the output of one powershell command and use it as the input to another powershell command. For example:
Get-Mailbox | Set-Mailbox -MaxSendSize 40MB
This will loop through every mailbox and then set the maximum send size to 40 MB in Exchange 2007.
...but the same doesn't work with get-rdremoteapp and new-rdremoteapp.
Get-RDRemoteApp | new-rdremoteapp -collectionname APPSNEW -connectionbroker edge-1.mydom.local
The goal of this command is that we are preparing to Migrate from a Windows 2012 RDS environment on virtual servers to a Windows 2012 R2 environment on physical servers.
On the virtual 'edge' server, I should be able to get all the RD Remote Apps, loop through them, and then use the 'new-rdremoteapp' command to create them on the new 'edge-1' server.
What actually happens is the command runs and creates the 1st remote app, then exits without an error. It doesn't process the apps in the list.
I think I need to use foreach-object, but after reading the docs and playing around, I can't seem to get it to work.
I couldn't find an easy out. I had to specify a bunch of parameters like so:
Get-RDRemoteApp | foreach-object -process {new-rdremoteapp -collectionname APPSNEW -connectionbroker edge-1.mydom.local -displayname $_.displayname -filepath $_.filepath -alias $_.alias -commandlinesetting $_.commandlinesetting -usergroups $_.usergroups}
Time to find a job that has more bash scripting... ;)

DeleteContents from mailbox with no confirmation (exchange 2010 and powershell)

I use the following command to delete the contents of a mailbox, but it still prompts me to confirm. Is there a way to not prompt me.
Search-Mailbox -id "UserName" -DeleteContent -Confirm:$false
The way to stop prompting is to use the -Force parameter (if the cmdlet supports it).
Setting -Confirm to $false just means "prompt me if you need to." Setting -Confirm to $true means "prompt me every time."
There seems to be a syntax error -confirm like -DeleteContent is a SwitchParameter this means that you just have to omit it.
Search-Mailbox -id "UserName" -DeleteContent
Simple thing just go through /Library/Mail/v2/MailData and delete the files with names
Envelope then all the contents will be deleted from Mail App