Share via


Using boolean properties with New-ADUser and Import-csv

Question

Friday, April 29, 2016 11:09 PM

Hello to all, I am trying to bulk import users with a script that I wrote which imports the properties from a .csv file, but when I run it, I get an error for the -Enabled property, which requires a Boolean input. I am still learning powershell, so I am open to any suggestions regarding the script. Thanks! :)

This is what the .csv looks like:
"Name","sAMAccountName","GivenName","Surname","Description","Departement","Enabled"
TestUser01,TestUser01,Test,User,Test account,IT,$true

This is the script:

Import-csv .\ADUsers.csv | ForEach-Object {

New-ADUser

 -name $_.Name -SamAccountName $_.sAMAccountname -GivenName $_.GivenName -Surname $_.Surname -Description $_.Description -Department $_.Departement `
-AccountPassword (Get-Content .\password.txt | ConvertTo-SecureString) -Enabled:$_.Enabled -ChangePasswordAtLogon $True -PassThru

}

and this is the error that I get:

New-ADUser : Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]' required by parameter 'Enabled'.

At C:\Test\CreateUsers.ps1:3 char:81

+ ... ring) -Enabled:$_.Enabled -ChangePasswordAtLogon $True -PassThru

+                    ~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.NewADUser

All replies (13)

Saturday, April 30, 2016 10:39 AM ✅Answered

Thank you all for the replies. Ultimately I was able to resolve the issue.

I had to change the Boolean values in the csv file from $true and $false to 1 and 0.

And in the code I had to rewrite it like this:

-Enabled:([bool]([int]$_.Enabled ))

I hope this can help someone in the future. :)


Friday, April 29, 2016 11:21 PM

-Enabled:([System.Convert]::ToBoolean($_.Enabled))

Friday, April 29, 2016 11:31 PM

I've tried it, but I get this error:

Exception calling "ToBoolean" with "1" argument(s): "String was not recognized as a valid Boolean."

At C:\Test\CreateUsers.ps1:2 char:1

+ New-ADUser -name $_.Name -SamAccountName $_.sAMAccountname -GivenName $_.GivenNa ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : FormatException


Friday, April 29, 2016 11:37 PM

Try with this

-Enabled ("$"+($_.Enabled))

Friday, April 29, 2016 11:44 PM

This is what I get:

New-ADUser : Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]'

required by parameter 'Enabled'.

At C:\Test\CreateUsers.ps1:3 char:81

+ ... ring) -Enabled $_.Enabled -ChangePasswordAtLogon $True -PassThru

+                    ~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Comman

   ds.NewADUser


Friday, April 29, 2016 11:49 PM

You cannot store a Boolean in a CSV.

-Enabled $($_.Enabled -eq 'True')

\(ツ)_/


Friday, April 29, 2016 11:56 PM

Soo there is no way to bulk import users and enable just some of them ?
^won't this fix the -Enabled property as true for all ? If yes, the won't it be easier just to write -Enabled:$true ?
Thanks! :)


Friday, April 29, 2016 11:57 PM

This is what I get:

New-ADUser : Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]'

required by parameter 'Enabled'.

At C:\Test\CreateUsers.ps1:3 char:81

+ ... ring) -Enabled $_.Enabled -ChangePasswordAtLogon $True -PassThru

+                    ~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Comman

   ds.NewADUser

You tried with $_.Enabled

Please try once with -Enabled ("$"+($_.Enabled))


Saturday, April 30, 2016 12:03 AM

This is the error message:

New-ADUser : Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]'

required by parameter 'Enabled'.

At C:\Test\CreateUsers.ps1:3 char:81

+ ... ring) -Enabled ("$"+($_.Enabled)) -ChangePasswordAtLogon $True -PassThru

+                    ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Comman

   ds.NewADUser


Saturday, April 30, 2016 12:14 AM

[bool]("$"+($_.Enabled))


Saturday, April 30, 2016 12:18 AM

New-ADUser : Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]'

required by parameter 'Enabled'.

At C:\Test\CreateUsers.ps1:3 char:81

+ ... ring) -Enabled [bool]("$"+($_.Enabled)) -ChangePasswordAtLogon $True -PassThru

+                    ~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Comman

   ds.NewADUser


Saturday, April 30, 2016 12:39 AM

It looks like your csv file has null values.


Saturday, April 30, 2016 12:45 AM

This is the whole csv file, I don't think it has null values.

"Name","sAMAccountName","GivenName","Surname","Description","Departement","Enabled"
TestUser01,TestUser01,Test,User,Test account,IT,$true
TestUser02,TestUser02,Test,User,Test account,IT,$true
TestUser03,TestUser03,Test,User,Test account,IT,$true
TestUser04,TestUser04,Test,User,Test account,IT,$true
TestUser05,TestUser05,Test,User,Test account,IT,$true
TestUser06,TestUser06,Test,User,Test account,IT,$true
TestUser07,TestUser07,Test,User,Test account,IT,$true
TestUser08,TestUser08,Test,User,Test account,IT,$true
TestUser09,TestUser09,Test,User,Test account,IT,$true
TestUser10,TestUser10,Test,User,Test account,IT,$true
TestUser11,TestUser11,Test,User,Test account,IT,$true
TestUser12,TestUser12,Test,User,Test account,IT,$true
TestUser13,TestUser13,Test,User,Test account,IT,$true
TestUser14,TestUser14,Test,User,Test account,IT,$true
TestUser15,TestUser15,Test,User,Test account,IT,$false