liveBook · Manning

String

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A string, represented by the .NET System.String type, is a sequence of characters representing text data.

A string is a sequence of characters used for text data. In PowerShell, a string is a System.String object, so it can be stored in variables, inspected, passed to parameters, and manipulated with methods or operators. section 3.5.4 section 16.2

Use strings in several common administration tasks:

  • Quote a string when it contains spaces. Single and double quotation marks are accepted, although single quotes are recommended for ordinary strings. section 3.5.4
  • Supply one or more strings to parameters. A String[] parameter accepts an array, and comma-separated values create an array; quote only individual values containing spaces. section 3.5.4
  • Inspect a string with Get-Member, then call methods such as IndexOf(), ToLower(), ToUpper(), Trim(), TrimStart(), and TrimEnd(). section 26.3
  • Convert between arrays and delimited strings with -join and -split. Use a delimiter such as a pipe or the tab escape sequence `t. section 26.2.3
  • Check the type of pipeline data before passing it to a string parameter. Extract a string property from an object when the command produces objects instead of String values. section 10.8

Clean and split administrator data

$username = " Sarah " $username.Trim() # Sarah $array = "one","two","three" $string = $array -join "|" # one|two|three $array = ($string -split "|") $array[0] # one

Avoid confusing strings with numbers

Read-Host returns a string. Multiplying the entered value "100" by 10 repeats the text rather than performing numeric multiplication. Declare the variable as [int] when numeric input is required; PowerShell then converts valid input and reports an error for values such as "Hello". section 16.6

    -Filter <String>
        Specifies a filter in the format or language of the provider. 
        The value of this parameter qualifies the Path parameter.
 
        The syntax of the filter, including the use of wildcard characters, 
        depends on the provider. Filters are more efficient than
        other parameters, because the provider applies them when the cmdlet 
        gets the objects rather than having PowerShell filter
        the objects after they are retrieved.
 
        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       False
        Accept wildcard characters?  true

Use the parameter listing to see how a String filter is accepted, interpreted by the provider, and applied to a path. section 3.5.4

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains quoting, arrays, object types, conversion, joining, splitting, and common methods through runnable command examples.
Examples
Demonstrates quoted paths, resource-group names, trimmed usernames, pipe-delimited arrays, tab-delimited files, and typed numeric input.
What it assumes
Assumes basic PowerShell commands and variables while teaching object types, quoting, arrays, and explicit conversion through examples.
What you can do after
Apply it via PowerShell operators and System.String methods to clean, inspect, split, join, store, and safely pass text values.
How it connects
Connects strings with variables, arrays, objects, parameters, Get-Member, pipelines, type conversion, and Azure resource groups.

Also explained in

Compare how 24 books explain string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.