Last active
September 22, 2016 05:50
-
-
Save Dan1el42/2e25c016101d1a106959e36d9f7948f2 to your computer and use it in GitHub Desktop.
Reply 01 for post https://powershell.org/forums/topic/string-output-manipualtion/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$osInfo = Get-WmiObject -Class Win32_OperatingSystem -Property Name | |
# Using the PowerShell -split operator to split the string into an array | |
# and output the first array item containing the operating system name | |
($osInfo.Name -split '\|')[0] | |
# Using the Split method of the .NET String class to split the string into an array | |
# and output the first array item containing the operating system name | |
$osInfo.Name.Split('|')[0] | |
# Using the IndexOf method to find the first occurence of the pipe symbol and than leverage | |
# the Substring method to extract the operating system name from the string | |
$osInfo.Name.Substring(0, $osInfo.Name.IndexOf('|')) | |
# Using the PowerShell -match operator with a regular expression to extract the operating system name | |
if ($osInfo.Name -match '^(.+?)\|') { $Matches[1] } | |
# Using the PowerShell -match operator with a regular expression | |
# and a named capturing group to extract the operating system name | |
if ($osInfo.Name -match '^(?<Caption>.+?)\|') { $Matches.Caption } | |
# Using the static Match method of the .NET Regex class to extract the operating system name | |
[System.Text.RegularExpressions.Regex]::Match($osInfo.Name, '^(.+?)\|').Groups[1].Value | |
# Using the static Match method of the .NET Regex class and a named capturing group to extract the operating system nam | |
[System.Text.RegularExpressions.Regex]::Match($osInfo.Name, '^(?<Caption>.+?)\|').Groups['Caption'].Value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment