Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test that a variable is more than eight characters in PowerShell?

How do test if the number of characters in $dbUserName is more than eight characters?

I have been unable to locate a command or series of commands that will let me do this. I have only been able to find if the variable is null:

if ($dbUserName) {     Write-Output " You left Username blank"     $dbUserName = read-host } 

But I would like to next test like this:

if ($dbUserName [String] > 8 ) }     Write-Output " Please enter more than 8 characters "     $dbUserName=read-host " Re-enter database user name" } 
like image 235
user1794918 Avatar asked Apr 28 '13 11:04

user1794918


People also ask

How do I find the length of a variable in PowerShell?

Use $string. Length to Get the String Length of a Variable in PowerShell. The $string. Length is the most straightforward method to check a string length of a variable in PowerShell.

What does $_ do in PowerShell?

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.

How do you echo a variable in PowerShell?

The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.

What type of variable is $$ in PowerShell?

The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable . The variable names are displayed without the preceding dollar ( $ ) sign that is used to reference variables. Variables are useful for storing the results of commands.


2 Answers

Use the length property of the [String] type:

if ($dbUserName.length -gt 8) {     Write-Output "Please enter more than 8 characters."     $dbUserName = Read-Host "Re-enter database username" } 

Please note that you have to use -gt instead of > in your if condition. PowerShell uses the following comparison operators to compare values and test conditions:

  • -eq = equals (==)
  • -ne = not equals (!=)
  • -lt = less than (<)
  • -gt = greater than (>)
  • -le = less than or equals (<=)
  • -ge = greater than or equals (>=)
like image 120
Ravi K Thapliyal Avatar answered Oct 25 '22 19:10

Ravi K Thapliyal


You can also use -match against a Regular expression. Ex:

if ($dbUserName -match ".{8}" ) {     Write-Output " Please enter more than 8 characters "     $dbUserName=read-host " Re-enter database user name" } 

Also if you're like me and like your curly braces to be in the same horizontal position for your code blocks, you can put that on a new line, since it's expecting a code block it will look on next line. In some commands where the first curly brace has to be in-line with your command, you can use a grave accent marker (`) to tell powershell to treat the next line as a continuation.

like image 22
user3524477 Avatar answered Oct 25 '22 19:10

user3524477