Reputation: 1396
Something like this :
if(GroupExists("Admins")) // <-- How can I do this line ?
{
([ADSI]"WinNT://$_/Admins,group").add($User)
}
else
{
$Group = Read-Host 'Enter the User Group :'
([ADSI]"WinNT://$_/$Group,group").add($User)
}
Upvotes: 2
Views: 4495
Reputation: 560
You can use the Exists static method as follows:
[ADSI]::Exists("WinNT://localhost/Administrators")
To get a True/False result you can wrap into try/catch statement.
$result = try { [ADSI]::Exists("WinNT://localhost/Administrators") } catch { $False }
or in a if/then statement you'll need to wrap it inside a $()
if ( $(try {[ADSI]::Exists("WinNT://localhost/Administrators")} catch {$False}) ) {
write-host "good"
}
else {
write-host "bad"
}
Upvotes: 8