Deleting Local Accounts Only Deletes Half In Powershell - powershell

I am creating hundreds of local accounts for testing purposes. The problem is cleanup, when I run the command to delete all the temp accounts, it only deletes half of them. "bca" is the account prefix, short for "bulk created account"
I run the count (everything done as admin)
Get-LocalUser | ? {$_.Name -like "bca*"} | Measure-Object
And I get count of 20
So I run the delete
Get-LocalUser | ? {$_.Name -like "bca*"} | Remove-LocalUser
Then I do the count again. Expecting 0. I get 10
I repeat the deletion, do the count, get 5, then 2, then 1 then finally 0.
UPDATE:
I was able to delete them all using the following command, from a comment below
foreach($user in Get-LocalUser -Name "bca*") { Remove-LocalUser $user}
BUT I am still curious why the original didn't work

Related

Find oldest email via PowerShell

I found multiple posts regarding this topic, but I don't think they are correct.
The following script works great if you want to find the earliest/oldest item (which for my luck, it is a contact).
Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity USERID |
Where OldestItemReceivedDate -ne $null |
Sort OldestItemReceivedDate |
Select -First 1 OldestItemReceivedDate
I want to find the earliest.. first email someone received. So someone helped me creating the following:
# Get the folder statistics for all folders
$stats = Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity $USERID
# Get the oldest email. Can re-use $stats for the other item types
$OldestEmail = $stats |
Where-Object {$_.OldestItemReceivedDate -and $_.ContainerClass -eq 'IPF.Note'} |
Sort-Object OldestItemReceivedDate |
Select-Object ContainerClass,OldestItemReceivedDate,FolderPath -First 1
# Outputs
ContainerClass OldestItemReceivedDate FolderPath
-------------- ---------------------- ----------
IPF.Note 2/8/2016 2:07:50 PM /Inbox
That previous script works great, if... the email is not on the Purge folder. For some reason, when the emails are moved to the Purge folder, the ContainerClass is also removed. I know there is an email prior to 2/8/2016, but it is on the -Archive \Recoverable Items\ Purge
If all you're doing is trying to avoid contact matching, change your ContainerClass to:
$_.ContainerClass -ne 'IPF.Contact'
That way you won't exclude the Purges folder. Alternatively you could adjust it to:
-and (($_.ContainerClass -eq 'IPF.Note') -or ($_.FolderPath -match "Purges"))
edit: I can't make comments to reply to you yet sorry, but yes Purges has no ContainerClass. It'll still proceed just fine with the 1st option (because a $null containerclass is still not equal to 'IPF.Contact'). Did you need to include contacts in the search?

PowerShell - Find Oldest Email

I am stuck, I am trying to find the oldest "EMAIL" in a person's mailbox, but I don't know what else to try. I think I need to add the ContainerClass -eq "IPF.Note" somewhere, but I am not sure where.
The following script works, but it finds the oldest ITEM, which in my case it is a contact. I want to look at each container (Email, Chats, Calendar, Contacts) separately, but for this script, I just want to know the oldest email.
Thank you
Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity USERID |
Where OldestItemReceivedDate -ne $null |
Sort OldestItemReceivedDate |
Select -First 1 OldestItemReceivedDate
You can filter what you have by item type, but I would do it after getting the statistics so you only have to query exchange once:
# Get the folder statistics for all folders
$stats = Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity $USERID
# Get the oldest email. Can re-use $stats for the other item types
$OldestEmail = $stats |
Where-Object {$_.OldestItemReceivedDate -and $_.ContainerClass -eq 'IPF.Note'} |
Sort-Object OldestItemReceivedDate |
Select-Object ContainerClass,OldestItemReceivedDate,FolderPath -First 1
# Outputs
ContainerClass OldestItemReceivedDate FolderPath
-------------- ---------------------- ----------
IPF.Note 2/8/2016 2:07:50 PM /Inbox
You are correct that the mailbox folder statistics command does not search recoverable items by default. It also does not search the mailbox archive unless you specify -Archive. If you need these, you'll have to do additional searches:
# Get recoverable items:
Get-MailboxFolderStatistics -Identity $USERID -FolderScope 'RecoverableItems' -IncludeOldestAndNewestItems |
Where-Object OldestItemReceivedDate |
Sort-Object OldestItemReceivedDate |
Select-Object ContainerClass,OldestItemReceivedDate,FolderPath -First 1
# Note that deleted item containers do not have an item type!
ContainerClass OldestItemReceivedDate FolderPath
-------------- ---------------------- ----------
2/5/2016 3:41:33 PM /Deletions
Presuming this is for compliance reasons to search a mailbox for items on an Exchange Server you should be using the Search-Mailbox cmdlet - https://learn.microsoft.com/en-us/powershell/module/exchange/search-mailbox?view=exchange-ps
For Exchange Online to search a mailbox for items you should use the New-ComplianceSearch cmdlet https://learn.microsoft.com/en-us/powershell/module/exchange/new-compliancesearch?view=exchange-ps
This web page shows how to search by date - New-ComplianceSearch: how to use the newer version of Search-Mailbox https://www.codetwo.com/admins-blog/new-compliancesearch-new-version-of-search-mailbox/
This web page has a script to search mailboxes, including dates PowerShell – New-ComplianceSearch script to go through all mailboxes, find a target message, and remove it - https://365basics.com/powershell-new-compliancesearch-script-to-go-through-all-mailboxes-find-a-target-message-and-remove-it/
Using your original approach, should be done like this. Presuming you have appropriate permissions.
Get-MailboxFolderStatistics -ID <mailboxemailaddress> -IncludeOldestAndNewestItems | select Identity, Name, FolderPath, ItemsInFolder, FolderSize, OldestItemReceivedDate | Export-Csv C:\temp\Mailbox.csv -NoTypeInformation

Get-EXOMailboxFolderStatistics timeout in large mailbox

When running Exchange Powershell "Get-EXOMailboxFolderStatistics" command on large mailbox (10's of thousands of folders) a timeout occurs and I have found no way of getting the results i need. My next step is to create a full command line application or UWP to achieve the administrative results needed.
Does anyone know of a way to retreive list of empty folders in this situation?
This works in normal mailboxes but returns timeout error in large mailbox:
Get-MailboxFolderStatistics $mailboxidentity |
Where {$_.identity -like "*Inbox*" -and $_.ItemsInFolder -eq 0} |
Select Identity |
Export-Csv -Path "c:\temp\emptyfolders.csv" -NoTypeInformation
Thanks!

Powershell command, to get users with expiring passwords in the next month or 30 days?

I'm having the hardest time getting the following output from powershell. The console just stops at the blinking cursor like the command is running, but I wait 20 min or so, and I still have no output, both in the powershell console, as well as when I try to export as a csv. I'm using the following command:
Search-ADAccount -AccountExpiring -DateTime "01/29/2017" | where {$_.ObjectClass -eq 'user'} | FT Name,ObjectClass -A | Export-Csv C:\temp
Could someone help? I've scoured the internet to no avail.
You are using format-table inappropriately. Don't use any Format-* cmdlets if you need to process the data after that point - formatting makes that impossible. Always save formatting for the very end, and only for user presentation.
Also, you're going to end up with a file in your C:\ root directory named temp that's not entirely usable as a CSV file, at least from Excel and other readers, because additional information is going to be inserted by Export-CSV. This will be eliminated by the -notypeinformation switch.
Additionally, you can speed this up by specifying the -UsersOnly switch for Search-ADAccount and skipping the where-object loop - the pipeline is really useful, but constructs like this can slow it down. Filter your data as far to the left as possible, and if you can do it inside a cmdlet that offers a filter, do it there.
Corrected script which should work as you expect:
Search-ADAccount -AccountExpiring -DateTime "01/29/2017" -UsersOnly | select-object -Property Name,ObjectClass | Export-Csv C:\temp\expiring.csv -NoTypeInformation;
Forgive me if this isn't perfect code, but this script will get you accounts expiring within the next 7 days. You can change the $DaysAhead variable to alter the time frame.
$maxPwdAge=(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days;
$daysAhead = 7;
$dateMin=(get-date).AddDays(-$maxPwdAge);
$dateMax=$DateMin.AddDays($daysAhead);
Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0} –Properties * | where {($_.PasswordLastSet) -ge $dateMin} |where {($_.PasswordLastSet) -le $dateMax} | select CN,EmailAddress,passwordLastSet | Format-Table;

office 365 Powershell

The HR department has 5000 unliscenced users. I want to remove them all.
I'm confused between two powershell commands and want to use the fastest one:
## 1
Get-MsolUser -UnlicensedUsersOnly | Remove-MsolUser -force
## 2
Get-MsolUser -All | where {$_.department -eq "HR"} | Remove-MsolUser -force
Although I don't think that there would be a huge difference in processing time since you're just using two different ways of retrieving a list of data, it seems like it would be a little faster to just pull the unlicensed users, as opposed to pulling all users and then filtering them based on department. However, are you sure that there aren't any unlicensed users in other departments that you may not want to delete?
Regarding which command is faster, have you tried using the Measure-Object command to see how long each one will take? You could just measure the Get-MsolUser command to confirm the difference.
Measure-Object {Get-MsolUser -UnlicensedUsersOnly}
Measure-Object {Get-MsolUser -All | where {$_.department -eq "HR"}}
Remove-MsolUser -force should take the same amount of time for both options. Also, I think you'll need to put the Remove-MsolUser command in a foreach loop:
foreach($user in Get-MsolUser -UnlicensedUsersOnly | where {$_.department -eq "HR"})
{Remove-MsolUser -ObjectId $user.ObjectId.guid -force}
https://technet.microsoft.com/en-us/library/ee176899.aspx