header image

Archive for December, 2018

PowerShell articles

Posted by: | December 31, 2018 Comments Off on PowerShell articles |

Here’s some links for other PowerShell articles I’ve written recently https://searchwindowsserver.techtarget.com/tip/PowerShell-commands-to-copy-files-Basic-to-advanced-methods https://searchwindowsserver.techtarget.com/tutorial/PowerShell-commands-for-Active-Directory-Groups-management https://searchwindowsserver.techtarget.com/tip/PowerShell-Core-61-offers-many-small-improvements https://searchwindowsserver.techtarget.com/tutorial/How-PowerShell-Direct-helps-polish-off-those-VMs https://searchwindowsserver.techtarget.com/tutorial/Working-with-PowerShell-module-downloads-from-the-gallery Enjoy

under: PowerShell

Change file times

Posted by: | December 30, 2018 Comments Off on Change file times |

When a file is created, accessed or modified that time is recorded. This is how to change file times. There are three properties to consider: CreationTime LastAccessTime LastWriteTime   Just to add to the fun there are UTC (GMT in real world) times as well: CreationTimeUtc LastAccessTimeUtc LastWriteTimeUtc   If you need to change a […]

under: PowerShell

Get first non-repeating character in a string

Posted by: | December 29, 2018 Comments Off on Get first non-repeating character in a string |

How do you get first non-repeating character in a string.   In Windows PowerShell v5.1 you could use Group-Object but as I showed last time that approach has been taken away in PowerShell v6.1.   Instead you need to loop through the characters in the string and count them function get-firstnonrepeatcharacter { [CmdletBinding()] param ( […]

under: PowerShell

Group-Object change in PowerShell v6.1

Posted by: | December 29, 2018 Comments Off on Group-Object change in PowerShell v6.1 |

There’s a subtle Group-Object change in PowerShell v6.1. In PowerShell v5.1 if you do this: $ts = ‘ffrluoluntlvxutxbdvbktgyyvsvcrkxoyfotzkzogcwuwycmnhuedk’ $ts.ToCharArray() | group   You get this: Count Name Group —– —- —– 3 f {f, f, f} 2 r {r, r} 3 l {l, l, l} 5 u {u, u, u, u…} 4 o {o, o, […]

under: PowerShell v5, PowerShell v6

Counting vowels

Posted by: | December 28, 2018 Comments Off on 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 […]

under: PowerShell

Create a random string

Posted by: | December 27, 2018 Comments Off on 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. […]

under: PowerShell

Return of the missing PSDiagnostics

Posted by: | December 12, 2018 Comments Off on 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 […]

under: PowerShell v6

Find duplicate characters in a string

Posted by: | December 12, 2018 Comments Off on 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 […]

under: PowerShell

Join-String

Posted by: | December 11, 2018 Comments Off on 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 […]

under: PowerShell v6

PowerShell v6.2 preview 3 install issue

Posted by: | December 11, 2018 Comments Off on 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 […]

under: PowerShell v6

Older Posts »

Categories