How to purge emails

this only apply for exchange on-prem, can be exchange 2010, 2013 or 2016

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ -TargetMailbox “Exch Admin” -TargetFolder “Purged01″ -LogLevel full

The switches –LogLevel full mean that the result of the search will be copied to the folder Purged01 into the mailbox Exch Admin.

If we don’t want to copy put the switches -LogLevel full –logonly and the end of the above command.

Example:

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ -TargetMailbox “Exch Admin” -TargetFolder “Purged01″ -LogLevel full –logonly

If we want to delete the content just put the switch –DeleteContent.

Example:

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ –DeleteContent.

How to get all users in Active directory and Exchange using PowerShell

When we are using Exchange PowerShell and we want to retrieve all mailboxes we can use
Get-mailbox –resultsize unlimited otherwise you will get only 10000 mailboxes.
PS C:\> Get-Mailbox -ResultSize Unlimited
Name    Alias  Database      ProhibitSendQuota   ExternalDirectoryObjectId                
—-    —-   ——-        —————–    ———————
If you want to filter the query you can use
PS C:\> Get-Mailbox | ?{$_.Office -eq ‘US’}
The question mark stands for “where-object” and it’s a way to short the query.
For Active Directory is a quite different, if you want to retrieve all users you have to use
  Get-AdUser –Filter * -Properties *
If you want to filter the query you can use
Get-AdUser –Filter * -Properties Department | where-object {$_.Department –eq “IT”}
In the same way as before, you can use ? also
  Get-AdUser –Filter * -Properties Department | ?{$_.Department –eq “IT”}

Using $? And !$? in PowerShell

 

   This is very helpful when you need to validate the last operation if it has information (values), in other words, if the last command was successfully.
For example, I want to validate if a folder exists in my computer:
PS C:\> $Folder = Get-ChildItem -Path c:\temp
if($?)
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
The folder Temp already exists
PS C:\> 
Let’s give it a try with a folder that doesn’t exist
PS C:\> $Folder = Get-ChildItem -Path c:\temp\NewOne
if($?)
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
Get-ChildItem : Cannot find path ‘C:\temp\NewOne’ because it does not exist.
At line:1 char:11
+ $Folder = Get-ChildItem -Path c:\temp\NewOne
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\NewOne:String) [Get-Chil
   dItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChi
   ldItemCommand
The folder Temp does not exists
PS C:\> 
Yo can use !$?also, just change the order in the if-else. I mean first the validation if the result will be false, it’s easier if we see this example:
PS C:\> $Folder = Get-ChildItem -Path c:\temp
if(!$?)
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
The folder Temp already exists
PS C:\> 
Now let’s try with a folder that does not exist
PS C:\> $Folder = Get-ChildItem -Path c:\temp\NewOne
if(!$?)
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
Get-ChildItem : Cannot find path ‘C:\temp\NewOne’ because it does not exist.
At line:1 char:11
+ $Folder = Get-ChildItem -Path c:\temp\NewOne
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\NewOne:String) [Get-Chil
   dItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChi
   ldItemCommand
The folder Temp does not exists
PS C:\>
Thanks for reading

 

How to get the folders size in windows

   Sometimes when we have some space problems, we need to know what information ewe must delete. The first step is get the folders with more space in the disk.
   There is not a fast way to know that, worst-case scenario you have to go through all folders one by one and right click to see the size used on disk.
   Researching on internet I found some PowerShell scripts to get folders details like name and size, but taking information from different sources I have created a script and even when I don’t get any error it takes more than an hour to finish, this is not a good idea for desperate people like me…. ¯\_()_/¯
This is the script… Not sure if it works because after an hour running  without any error, it never finishes.
But as part of the research I found a very interesting site with a bunch of Microsoft tools, one of them is “Disk Usage”  a very fast and reliable tool
After download and unzip the tool, this is the result:
You can copy and paste the information in a spreadsheet and you will be able to see the information as follow:
Adding a column to convert the Size on Disc to MB

How to see Windows time settings

   The command W32tm /query /configuration works in all windows versions and its helpful when a server takes another timeserver and can be different from other servers or computers running an application or service and there is a time discrepancy.
   There are more tools and different parameters to use with that command but sometimes with /query /configuration are enough.  

for more details you can visit https://docs.microsoft.com/en-us/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings

How to connect powershell to Skype for business online

To use all cmdlets for Skype, you must install the module, first download from Microsoft:

Click on the .exe file to install
Basically, is Next – Next – Finish
Once the module is installed, open your powershell as administrator.
To Create the session type these lines:
#only once
#Set-ExecutionPolicy RemoteSigned
$Cred = Get-Credential
$Session = New-CsOnlineSession -Credential$Cred
Import-PSSession $Session
NOTE: only the first time delete # in order to uncomment the second line to allow execute scripts. After that, you can comment this line again, is not needed the next time you execute this script to create the session.
Input the credentials to open the session
Once the session is established, the prompt will appear
And, you can type get-CsOnlineUser UserName and you will see all the user properties.

And that’s it. You can start using all cmdlets for Skype online.

Process to purge emails in office 365

   When an email with malicious content like virus, phising, spam, etc is coming to the company we need to delete those emails from the recipients Inboxes, sometimes in the companies, an employee send an email with incorrect or offensive content, this process applies as well.
Please follow this process:  

1. create the content. (based on https://docs.microsoft.com/en-us/office365/securitycompliance/content-search)             



Click on Content search
Create a new Search
Here we can put the conditions like sender, date range, subject, attachment, etc.
Click on save and run, its important input a name for this search.
In the open option, we can see all the searches saved
Once the search has finished, go to powershell and use the cmdlet New-ComplianceSearchAction
NOTE: to use the compliance module execute this script
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationNameMicrosoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/-Credential $UserCredential-Authentication Basic-AllowRedirection
Import-PSSession $Session -DisableNameChecking
You can validate the report and this task has been completed.

Problems to install Skype for business online powershell module in windows 10

When I was trying to install the Skype for business online I was getting different errors, the first was
“must have C++ Redistributable (x64) – 14.10.25008 installed as a Minimum Runtime.”
while I was trying to install the module.

If I tried to import the module appears the follow error:
“import-module : the specified module ‘skypeonlineconnector’ was not loaded because no valid module file was found in any module directory.” 
The solution is download the latest supported C++, you can find it here:
After install, reboot the computer and you will be able to install the skype for business module without any problems.

To connect to Skype online, type the follow lines

Import-Module SkypeOnlineConnector
$userCredential = Get-Credential
$sfbSession = New-CsOnlineSession -Credential $userCredential
Import-PSSession $sfbSession

and Listo!

to make sure that you have the module ready, you can type Get-CsOnlineUser Username, for example:

and you will see the properties for that user.

I hope it helps.

How to see privates emails in outlook

   Recently I had a problem where an user was not able to see the emails sent with sensitivity “Private”, the user was able to see these emails in outlook on the web, but in outlook app he only sees how the unread items was increasing, but he didn’t realize why..
Just to validate, the first step is verify if the user has permissions in the shared, folder.
For this, we can go to Admin Exchange Recipients Shared.
In this case, we have a shared mailbox called Marketing. To validate the permissions, go to full access and check if the user appears.

The user test user2 has full access permissions, then so far so good.
In order to solve the issue, go to outlook app in the user computer (No Outlook on the web)
Go to search in the user computer
You need to configure outlook with the shared mailbox settings, in this case Marketing.
NOTE: Could be easier if you reset the windows password, create a windows profile and open outlook, in automatic will be created its outlook profile.
Once the outlook profile has been configured for the shared mailbox.
Go to File menu – Account SettingsDelegates
Add the user who reports the issue, but the most important is check the option on “Delegates can see my privates items”
Close outlook and ask the users restart outlook and they will be able to see the emails.

How to import group policy module in powershell

 

Even if you have the RSAT tools installed in your computer, the Group policy module is not working. If you type a command like get-gpo –all –domain MyDomain.com you will receive an error like this:
get-gpo : The term ‘get-gpo’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
To fix this problem, go to start menu and search for optionalfeatures, as shown below:
Then, you will see Windows features window appear.
Expand Remote Server Administration Tools – Feature Administration tools and select Group Policy Management Tools:
Wait for the installation
Once the installation is done, go to Power Shell and import the module. Using Import-Module grouppolicy command
PS C:\> Import-Module grouppolicy
PS C:\> 
Now we have all GPO commandlets available
If you want to see all commands that belong for this module, type Get-Command –module grouppolicyand hit enter

Enjoy It.