help with $true $flase variable

Hello All,

New here.


I am trying to get a prompt so that the user can type $true or $flase if they wanted the account to enabled or disabled, i got it working as INT using number 0 or 1 but not as $true or $false, can you please help?




    [switch]$enabled = $( Read-Host "input 0 or 1" ),

    [Parameter(Mandatory=$True, HelpMessage="Enter the name")]



    [Parameter(Mandatory=$True, HelpMessage="Enter the Given name")]



    [Parameter(Mandatory=$True, HelpMessage="Enter the Surname")]



    [Parameter(Mandatory=$True,HelpMessage="Enter the Account name")]



    [Parameter(Mandatory=$True, HelpMessage="Enter the User Principle name, has to be first initial[dot]lastname e.g 'M.test'")]


    [string][ValidateLength(3,15)]$UPN = 02


#Imports AD Moudle

Import-Module ActiveDirectory

#This script lets you create AD user accounts

New-ADUser -Name $name  -GivenName $GivenName -Surname $Surname -SamAccountName $Accountname -UserPrincipalName $UPN -Path "OU=test,DC=test,DC=local" -AccountPassword(Read-Host -AsSecureString "Input Password") -Enabled $enabled

Should not be converting it to int. Use bool instead.

btwn, please read the code posting steps while posting code in the forum.

The problem is that you’re only going to get a string object from Read-Host, not a boolean.

PS E:\Temp> $test = Read-Host "$true or $false"
True or False: $true
PS E:\Temp> $test | Get-Member

   TypeName: System.String

If you want to use Read-Host (I wouldn’t, you’re better off using parameters), then you’ll need to convert it. However, you can’t cast a string to a boolean e.g. this won’t work:

[bool]$enabled = '$true'

An int can be cast to a boolean so you could cast the string from Read-Host to an int and cast the int to a boolean:

[bool]$enabled = [int](Read-Host "Enter 1 to enable, or 0 to disable")

Really though, as you’re already using parameters, just add another one for enabling the account.

Sorry i am new to this forum, Thanks for the help Matt.