hi all,

i have a powershell script that looks at a csv file with columns like

fn,ln,pw,dom,ofc,dist

now most of them are populated but “dist” columns arnt as some users dont want to be added to any dist group on o365

how can i do this please, i thought about an if statement within my foreach loop, like so, il give you a snippet

$maildist = $mail.dist

if ($maildist -eq “value”){

Add-DistributionGroupMember -Identity $maildist -Member $mailids}

obviously theres more to the script but you get the idea

so if the cell contains a value do the command, and if it doesnt, dont run the command

thanks,

rob

12 Spice ups

think you need to use the ISNULL function, assuming the cell truly has no value and not a space or 0 or something like that

yeah i leave the dist column blank if the user doesnt want to be added to any dis group, otherwise i put in ie maildist@domain.com

what about this

if ($maildist -ne “”){

from memory that will only be true if it’s a text value with nothing in it. I don’t think it would work, but give it a test. If it doesn’t, try the if(isnull(cell), function

thats what i want to do

if contains any value do the command or if blank dont do the command

think i found the answer

$str2 = ‘’

if ($str2) { ‘not empty’ } else { ‘empty’ }

empty

Robert, when posting code please use the insert code option </> and select the type of code - in your case, PowerShell.

It’ll format and read a lot easier.

1 Spice up

sorry Rod, will do in future, my bad, but anyway, i tested my script and works perfect, once im done with it, il share on here :slight_smile:

Nothing to be sorry about, it just makes it easier for people to see code vs text.

How can I do two variables that come back as ne in an if statement

Or do I have to do another if variable ne statement again

I think this is what you are asking for, you can trail multiple “and” or “Or” statements in the if. Here are a few examples:

if ($null -ne $csv.dist -and $null -ne $csv.group -or $csv.dist -contains 'mail') {
#Do something
}

You can check both variables for data like so and perform your action if both are found.

if ($variable1 -and $variable2) {
#Do something
}

@jayparker9836 ​ what you said in the other thread works

if ($maildist){
Add-DistributionGroupMember -Identity $maildist -Member $mailids}
if ($mailo){
Set-Mailbox -Identity $mailas -Office $mailo}
if ($mail365){
Add-UnifiedGroupLinks -Identity $mail365 -LinkType Members -Links $mailids}

1 Spice up