1

I have to create a script to get all EoL Windows computers in our AD.

This is my Code right now:

$getad = Get-ADComputer -Filter {
OperatingSystem -like "Windows 10*"
-or 
OperatingSystem -like "*Windows Vista*"
-or 
OperatingSystem -like "*Windows XP*"
-or 
OperatingSystem -like "*95*" 
-or  
OperatingSystem -like "*94*"     
-or       
OperatingSystem -like "*Windows 8*"      
-or        
OperatingSystem -like "*Windows 8.1*"
-or         
OperatingSystem -like "*2000 Professional*"        
-or            
OperatingSystem -like "*2000 Server*"         
-or           
OperatingSystem -like "*2003*"             
-or            
OperatingSystem -like "*Windows NT*"               
-or           
OperatingSystem -like "*Windows 7*"            
-and 
#Windows8         
OperatingSystemVersion -notlike "*6.3.9600*"           
-and 
#Windows7 SP1            
OperatingSystemVersion -notlike "*6.1.7601*"             
-and 
#Windows10               
OperatingSystemVersion -notlike "*16299*"  
-and 
#Windows10                
OperatingSystemVersion -notlike "*14393*"                 
-and 
#Windows10                 
OperatingSystemVersion -notlike "*15063*"                
} -Properties ('Name', 'operatingsystem', 'DistinguishedName', 
'description', 'lastlogondate', 'OperatingsystemVersion') 
$selectobj = $getad | Select-Object Name, Operatingsystem, 
DistinguishedName, Description, Lastlogondate, OperatingSystemVersion
$selectobj

The problem: The part with -notlike is not applied. I get computers with the versions I do not want to see.

I need all EoL Computers in one variable so i can work with them.

2 Answers 2

2

The problem is one of logic with your combination of or and and, but don't use -like and -notlike they don't work the way you think. Use the regular expression switches -imatch and -inotmatch like this:

OperatingSystem -imatch "Windows 10|Windows Vista|Windows XP|95|94|Windows 8|2000|2003|Windows NT|Windows 7"
-and OperatingSystemVersion -inotmatch "6.3.9600|6.1.7601|16299|14393"
Sign up to request clarification or add additional context in comments.

1 Comment

If i use -imatch instead of -like i get an error. I've replaced my -like and -notlike part with the -imatch and -inotmatch. But now i get the error: 'Operator Not supported: -imatch'
0

I added ( and ) and now it works.

 $getad = Get-ADComputer -Filter {(operatingsystem -like "*Windows 10*" -and OperatingSystemVersion -notlike "*16299*" -and OperatingSystemVersion -notlike "*14393*" -and OperatingSystemVersion -notlike "*15063*") -or (operatingsystem -like "*Windows Vista*") -or (operatingsystem -like "*Windows XP*") -or  (operatingsystem -like "*95*") -or (operatingsystem -like "*94*") -or ( operatingsystem -like "*Windows 8*" -and OperatingSystemVersion -notlike "*9600*") -or (operatingsystem -like "*2000 Professional*") -or (operatingsystem -like "*2000 Server*") -or (operatingsystem -like "*2003*") -or (operatingsystem -like "*Windows NT*") -or ( operatingsystem -like "*Windows 7*" -and OperatingSystemVersion -notlike "*7601*")} -Properties ('Name', 'operatingsystem', 'DistinguishedName', 'description', 'lastlogondate', 'OperatingsystemVersion', 'Created', 'Enabled', 'SamAccountName')

 $selectobj = $getad | Select-Object Name, Operatingsystem, DistinguishedName, Description, Lastlogondate, OperatingSystemVersion, Created, Enabled, SamAccountName

It works but it isn't nice at all because of its lenght. Is there any other shorter way?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.