tl;dr:
Call the .ToLower()
method directly on string values for to-lowercase conversions:
# A simpler way to extract the ID
$id = if ((git show) -join "`n" -match '\sid="(.+?)"') { $Matches[1] }
# Note the the .ToLower() call and the enclosure in $(...)
# I've omitted Write-Output here, because it isn't necessary.
# Since the string isn't assigned to a variable or sent elsewhere, it
# is *implicitly* output.
"##vso[task.setvariable variable=BID]$($id.ToLower())"
$()
, the subexpression operator is needed for embedding expressions that involve property access or method calls inside "..."
(expandable strings).
PowerShell is built on .NET and uses its data types (classes), instances of which are objects that notably have properties and methods.
This rich type system is at the heart of PowerShell is what distinguishes PowerShell from other shells.
Strings, as returned by an external program such as git
in this case, are of type [string]
(System.String
), and this type has a .ToLower()
method for returning a lower-cased copy of the string.
PS> 'aBC'.ToLower()
abc
Unsurprisingly, there's also a .ToUpper()
method.
If the input value isn't a string, you can convert it to one by calling the ToString()
method, which instances of any .NET type support:
# Get-Item returns a System.IO.DirectoryInfo instance.
# You must convert it to a string first, before you can call ToLower()
PS> (Get-Item $HOME).ToString().ToLower()
C:\users\jdoe
Note that calling .ToString()
does not give you the same rich string representation that you would see in the console (terminal). The latter uses PowerShell's output-formatting system, and to use it you have to pipe to the Out-String
cmdlet:
PS> (Get-Item $HOME | Out-String).ToLower()
directory: c:\users
mode lastwritetime length name
---- ------------- ------ ----
d----- 2/3/2020 10:18 am jdoe
Out-String
returns a single, multi-line string by default; use the -Stream
switch to make it generate line-by-line output (which becomes a string array when collected).
If you want to examine a variable value's / command result's data type, pipe to the
Get-Member
cmdlet:
PS> Get-Item $HOME | Get-Member
TypeName: System.IO.DirectoryInfo
Name MemberType Definition
---- ---------- ----------
LinkType CodeProperty System.String LinkType{get=GetLinkType;}
Mode CodeProperty System.String Mode{get=Mode;}
...
The TypeName:
line contains the full .NET type name, followed by detailed information about the type's members, notably properties and methods.
There are many options for showing only type members of interest, such as -Type Method
to only show methods.