locked
How to Filter for Accounts by Domain RRS feed

  • Question

  • I have this script to delete accounts from servers that haven't been modified for 30 days.  I need to make it filter only on accounts that are from a specific domain like domainA\user0.  It needs to exclude any other domain accounts like domainB\user1.  Any help is appreciated.

    $server="server0.domain.com"
    $credential=Get-Credential
    Invoke-Command -ComputerName $server -Credential $credential -ScriptBlock {
    Write-Warning "Filtering for user profiles older than $Days days"
    Get-CimInstance win32_userprofile -Verbose |
    Where {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-30)} |
    Remove-CimInstance -Verbose

    TIA

    Monday, November 13, 2017 1:13 PM

All replies

  • Hi,

    Are you getting any error while running this script?


    (66,65,83,65,84,73|%{[char]$_})-join''

    Monday, November 13, 2017 1:28 PM
  • Hi, 

    Get-ADUser -filter {lastLogon -le $(Get-Date).Date.AddDays(-30)} -LDAPFilter "(name=*)" -SearchScope Subtree -SearchBase "DC=rootandadmin,DC=com" 
    kind regards



    S'il vous plaît n'oubliez pas de =Marquer comme réponse= les réponses qui ont résolu votre problème. C'est une voie commune pour reconnaître ceux qui vous ont aidé, et rend plus facile pour les autres visiteurs de trouver plus tard la résolution. Héritier KANDOLO. http://www.rootandadmin.com/

    Monday, November 13, 2017 2:05 PM
  • Use ActiveDirectory module, if in a client OS, install RSAT and use ActiveDirectory module

    Regards kvprasoon

    Monday, November 13, 2017 5:56 PM
  • I'm not getting an error.
    Monday, November 13, 2017 6:38 PM
  • $s = New-PSSession server0.domain.com -Credential
    Get-CimInstance win32_userprofile -CimSession $s |
        Where-Object { $_.LastUseTime -lt $(Get-Date).Date.AddDays(-30) } |
    ForEach-Object{ $_.Delete() }

    CIM does its own remoting.

    You cannot correctly delete profiles using "Remove"


    \_(ツ)_/

    Monday, November 13, 2017 7:23 PM