Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

When a filter contains a variable, like $date

$
0
0

I’m try to filter out users in an OU where, for example, their name starts with Del_May2015 which is April + one month.  This is because after the 15th of the month the users in the OU will have April + two moths so Del_June2015.  I only want the Del_May2015 users returned so I can purge them and not the Del_June2015 accounts, they will be purged in June.  As this script runs automatically I want it to build the filter dynamically, which is where I’ve come unstuck. I tried two different ways to do it but I get nothing returned.

 

So how do I filter on Del_$Date, Users Get-ADUser output is below

 

 

#Contain where the search will run through

$OU="OU=ToProcess,OU=Staff-Ex,OU=Users,OU=Been,DC=Here,DC=There,DC=uk"

$Date=(get-date).addmonths(1).tostring('MMMMyyyy')

 

#Get all the users who's username starts with Del_$date and output to a file for record keeping

get-aduser-filter*-searchbase$OU-properties*|where-object {$_.Name  -eq"Del_$date*"} |export-csv"C:\$date.Test.csv"

 

get-aduser-filter {Name  -like"Del_$Date*"} -searchbase$OU-properties*|export-csv"C:\$date.Test.csv"

 

 

DistinguishedName : CN=Del_May2015-Goodall\, Tim,OU=ToProcess,OU=Staff-Ex,OU=Users,OU=Been,DC=Here,DC=There,DC=uk

Enabled           : False

GivenName         : Tim

Name              : Del_May2015-Goodall, Tim

ObjectClass       : user

ObjectGUID        : c8d70d19-5fef-49c0-ad4f-110fdc2af35b

SamAccountName    : cyctg

SID               : S-1-5-21-777456390-1647073903-355810188-32994

Surname           : Goodall

UserPrincipalName : cyctg@here.there.uk

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles