Counting vowels

If you’re given a string how would you go about counting vowels, consonants  and non-alphabet characters.

My approach would be:

function measure-vowel {
   [CmdletBinding()]
   param (
     [string]$teststring
   )

  $counts = [ordered]@{
      Vowels      = 0
      Consonants  = 0
      NonAlphabet = 0
   }
  
   $vowels = 97, 101, 105, 111, 117   
  
   $teststring.ToLower().ToCharArray() |
   foreach {
      $test = [byte]$psitem

     switch ($test) {
        {$test -in $vowels}               {$counts.Vowels += 1; break}
        {$test -ge 97 -and $test -le 122} {$counts.Consonants += 1; break}
        default                           {$counts.NonAlphabet += 1; break}
      }

  }

  New-Object -TypeName PSObject -Property $counts

}

The string to test is an input parameter.  Set up the output hash table and the byte value of the vowels.

Convert the string to lowercase and then to a char array which is put on the pipeline. Convert the char to a byte value and then use the switch statement to determine if its a vowel, consonant or non-alphabetic character.

Finally output the results.

Advertisements
Posted in Powershell | 2 Comments

Create a random string

I often need to create a random string of characters. Here’s a simple function to achieve that:

function get-randomstring {
   [CmdletBinding()]
   param (
     [int]$length
   )

  $rca = 1..$length |
   foreach {
     $ran = Get-Random -Minimum 97 -Maximum 123
     [char][byte]$ran
   }

  $rca -join ”

}

The required length is passed as an integer parameter.  Use the range operator as a counter and in Foreach-Object generate a random number between 97 and 122.  Convert that to a char  (a=97 and z-122) and add to the array.  Join the array of chars to create the string.

Posted in Powershell | 2 Comments

Return of the missing PSDiagnostics

Return of the missing PSDiagnostics members in PowerShell v6.2 preview 3.

In Windows PowerShell v5.1 the PSDiagnostics module contains these members:

Disable-PSTrace
Disable-PSWSManCombinedTrace
Disable-WSManTrace
Enable-PSTrace
Enable-PSWSManCombinedTrace
Enable-WSManTrace
Get-LogProperties
Set-LogProperties
Start-Trace
Stop-Trace

In PowerShell v6.1.1 you just get these:

Disable-PSTrace
Enable-PSTrace
Get-LogProperties
Set-LogProperties

Quite a few missing.

In PowerShell v6.2 preview 3 we’re back to the original set:

Disable-PSTrace
Disable-PSWSManCombinedTrace
Disable-WSManTrace
Enable-PSTrace
Enable-PSWSManCombinedTrace
Enable-WSManTrace
Get-LogProperties
Set-LogProperties
Start-Trace
Stop-Trace

NOTE: this is just on Windows – I don’t have a non-Windows machine to determine what’s available on  non-Windows platforms

Posted in PowerShell v6 | Leave a comment

Find duplicate characters in a string

Staying with our current theme of manipulating strings this is how you find duplicate characters in a string.

function get-duplicatechar {
   [CmdletBinding()]
   param (
     [string]$teststring
   )
  
   $teststring.ToCharArray() |
   Group-Object -NoElement |
   where Count -gt 1 |
   Sort-Object -Property Count -Descending

}

Convert the string to a char array, group on the characters and use Where-Object to filter the characters that occur more than once – the duplicates. Sort the output for easier viewing.

Posted in Powershell | Leave a comment

Join-String

Join-String is a new cmdlet in PowerShell v6.2 preview 3. Join-String enables you to use the pipeline to join strings.

We’ve had the –join operator for a long time:

PS>  1..3 -join ‘,’
1,2,3

As an alternative you could do

PS>  1..3 | Join-String -Separator ‘,’
1,2,3

You can add a prefix and suffix to the final string:

PS>  1..3 | Join-String -OutputPrefix ‘A’ -OutputSuffix ‘b’ -Separator ‘,’
A1,2,3b

You can use the property of objects on the pipeline as a source for the data:

PS>  Get-ChildItem -Path C:\Scripts\Techniques\ | select -ExpandProperty Basename | Join-String -Separator ‘,’
arrays,numbertechniques,stringtechniques

Quotes – single or double – can be added

PS>  Get-ChildItem -Path C:\Scripts\Techniques\ | select -ExpandProperty Basename | Join-String -Separator ‘,’ -SingleQuote
‘arrays’,’numbertechniques’,’stringtechniques’

PS>  Get-ChildItem -Path C:\Scripts\Techniques\ | select -ExpandProperty Basename | Join-String -Separator ‘,’ -DoubleQuote
“arrays”,”numbertechniques”,”stringtechniques”

The current culture can also be use to influence the string. For instance dates:

PS>  Get-Date

11 December 2018 16:25:43

PS>  Get-Date | Join-String
12/11/2018 16:25:52

To me the string version of the date reads  12 November 2018 not 11 December 2018 so I need to use my culture

PS>  Get-Date | Join-String -UseCulture
11/12/2018 16:26:54

And now it looks correct.

I suspect that Join-String will become one of those cmdlet that has many, many uses. For instance an easy way to create sequential file names:

PS>  1..5 | foreach {$_ | Join-String -OutputPrefix ‘File’ -OutputSuffix ‘.txt’}
File1.txt
File2.txt
File3.txt
File4.txt
File5.txt

Posted in PowerShell v6 | 1 Comment

PowerShell v6.2 preview 3 install issue

PowerShell v6.2 preview 3 install issue – PowerShell v6.2 preview 3 is now available from https://github.com/PowerShell/PowerShell/releases but you may notice a probloem if you install over the top of PowerShell v6.2 preview 2. 

When you click on the icon to start preview 3 the console will flash open and then immediately close.

The fix is to either uninstall preview 2 before installing preview 3 OR install preview 3 over the top of preview 2 and immediately rerun the preview 3 installation selecting the repair option. The install will work through and then PowerShell v6.2 preview 3 will work.

the underlying cause is understood and hopefully will be fixed in a later release

Posted in PowerShell v6 | Leave a comment

Count the occurrence of given character in a string

Last time I showed how to get the number of occurrences of each character in a string but how do you count the occurrence of given character in a string?

You use one of the fundamental concepts on which PowerShell is built – its composability. In other words PowerShell is composed of a lot of very small pieces of code that each do its own job and you select and assemble the components  you need to complete a given task.

What that boils down to is that you already have a function that counts the number of occurrences of a character in a string. Rather than writing another function, or spending time modifying your existing function, see if you use other PowerShell cmdlets (or functions) help you get the result you need. In this case:

PS> measure-occurrence -teststring  ‘cwfhgfhcdsgfchgfegfegfkvcnfdhvjewy\dfsa’ | where Name -eq ‘s’

Count Name
—– —-
     2 s

Gives you the result you want without any further work.

Two ways to use the function makes it more cost effective to write.

Posted in Powershell | 3 Comments