<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Strings_%28PowerShell%29</id>
	<title>Strings (PowerShell) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Strings_%28PowerShell%29"/>
	<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Strings_(PowerShell)&amp;action=history"/>
	<updated>2026-05-20T08:30:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://vwiki.co.uk/index.php?title=Strings_(PowerShell)&amp;diff=2325&amp;oldid=prev</id>
		<title>Sstrutt: Initial creation - content from Variables page</title>
		<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Strings_(PowerShell)&amp;diff=2325&amp;oldid=prev"/>
		<updated>2013-06-21T14:39:56Z</updated>

		<summary type="html">&lt;p&gt;Initial creation - content from Variables page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Basic manipulation tasks can be carried out by using the string object&amp;#039;s methods, eg &amp;lt;code&amp;gt; &amp;quot;string&amp;quot;.PadRight(10) &amp;lt;/code&amp;gt;, see &amp;lt;code&amp;gt; Get-Member -InputObject &amp;quot;Text&amp;quot; &amp;lt;/code&amp;gt; for full details.&lt;br /&gt;
&lt;br /&gt;
== Escape Characters ==&lt;br /&gt;
{|class=&amp;quot;vwikitable&amp;quot;&lt;br /&gt;
|- &lt;br /&gt;
!  Text              !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `0 &amp;lt;/code&amp;gt;  || Null (also $null)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `a &amp;lt;/code&amp;gt;  || Bell/system beep&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `b &amp;lt;/code&amp;gt;  || Backspace&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `f &amp;lt;/code&amp;gt;  || Form feed&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `n &amp;lt;/code&amp;gt;  || New line&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `r &amp;lt;/code&amp;gt;  || Carriage return&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `t &amp;lt;/code&amp;gt;  || Tab (horizontal)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `v &amp;lt;/code&amp;gt;  || Vertical tab&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `&amp;#039; &amp;lt;/code&amp;gt;  || &amp;#039;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt; `&amp;quot; &amp;lt;/code&amp;gt;  || &amp;quot;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Concatenation + ==&lt;br /&gt;
Strings can be concatenated together using the &amp;lt;code&amp;gt;+&amp;lt;/code&amp;gt; operator, however its also possible to use interpolation within string, the following produce identical results...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$strAB = $strA + $strB                    # Concatenation&lt;br /&gt;
$strAB = &amp;quot;$strA $strB&amp;quot;                    # Interpolation&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Interpolation ==&lt;br /&gt;
Interpolation allows variables to be embedded into a string and to be resolved into their actual values.  This works between double quotes, but not between single quotes...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
PS E:\&amp;gt; $sub = &amp;quot;replaced&amp;quot;&lt;br /&gt;
PS E:\&amp;gt; Write-Output &amp;quot;Variable has been $sub&amp;quot;&lt;br /&gt;
Variable has been replaced&lt;br /&gt;
PS E:\&amp;gt; Write-Output &amp;#039;Variable has been $sub&amp;#039;&lt;br /&gt;
Variable has been $sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that you can&amp;#039;t use interpolation when using string properties of an object....&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$strAB = $strA.text + $strB.text           # Works&lt;br /&gt;
$strAB = &amp;quot;$strA.text $strB.text&amp;quot;           # Fails&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Search ==&lt;br /&gt;
To search for specific text in a string...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if (Select-String -InputObject $text -Pattern &amp;quot;StringToFind&amp;quot; -Quiet)&lt;br /&gt;
{&lt;br /&gt;
        # StringToFind found in $text&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Match (basic) ==&lt;br /&gt;
To do a basic search/match...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if ($res.Contains(&amp;quot;Success&amp;quot;)) {&lt;br /&gt;
    # String did contain&lt;br /&gt;
} else {&lt;br /&gt;
    # Didn&amp;#039;t&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
...which is much preferable to...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
if ($res.CompareTo(&amp;quot;Success&amp;quot;)) {&lt;br /&gt;
    # Didn&amp;#039;t match (CompareTo returns 1 if comparison fails !)&lt;br /&gt;
} else {&lt;br /&gt;
    # Did match&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Match (extract) ==&lt;br /&gt;
To extract text that matches a regex...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$res = [regex]::matches($line, &amp;quot;\d{4}-[A-Za-z]{3}-Week\d{1}.log&amp;quot;)&lt;br /&gt;
if (-not $res.Count)&lt;br /&gt;
{&lt;br /&gt;
    # No matches found&lt;br /&gt;
} else {&lt;br /&gt;
    $res1 = $res.Item(1).Value      # 1st match to regex&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
See [[Regular Expressions]] for further info on regex stuff.&lt;br /&gt;
&lt;br /&gt;
== Replace ==&lt;br /&gt;
Basic find and replace can be done with a string&amp;#039;s Replace method, eg to replace &amp;quot;\&amp;quot; with &amp;quot;\\&amp;quot; in the $query variable...&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt; $query = $query.Replace(&amp;quot;\&amp;quot;, &amp;quot;\\&amp;quot;) &amp;lt;/source&amp;gt;&lt;br /&gt;
For proper regular expressions support, use the following syntax&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt; $query = [regex]::Replace($query, &amp;quot;search&amp;quot;, &amp;quot;replace&amp;quot;) &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Strip Whitespace ==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt; $string = $string.TrimEnd() &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[category:PowerShell]]&lt;br /&gt;
[[category:Variables (PowerShell)]]&lt;/div&gt;</summary>
		<author><name>Sstrutt</name></author>
	</entry>
</feed>