Strings in powershell

So First lets start by looking at what is a String in Powershell? The Technical definition is basically a datatype that comes from the System.Object. That’s a concept to hard to grasp basically a group a of letters put together would form a string. so whenever you see the double quotes or single quotes with a word inside of it that would be a string. ” word!” on the back end, a lot of the Objects are of String type but powershell converts a lot of that stuff for us without you even noticing.

Anyway…

Task1: Research about Strings in Powershell

at the most basic level below there is a one example of a variable name called $favoriteFood, with a String value of “pizza”

 $favoriteFood = 'pizza'

You can also use double quotes.

$favoriteFood = "pizza"

Task2: Create Six Strings inside an array the array name would be Colors

To confirm that what you created are strings you can use the GetType() method. example:

$favoriteFood.GetType()

You can type strings also without assigning them to a variable. example

"pizza" 

There are different ways that you can format and create strings Using the static Format method.

[string]$FavoriteFood = "Pizza"
[string]::Format("FavoriteFood = {0}", $favoriteFood)

For those of you that have been programming for a while this kind of looks like C language in a way, also if you used the .NET framework then you will understand it is common to format strings this way.

Another way to Format the string is by using the -f Windows Powershell format operator. example below.

"Favorite Pizza = {0}" -f $favoriteFood

Another way to format is using the Expanded String Method for example:

"favorite food = $favoriteFood"

Task3: Create 5 String variables of pizza toppings and display the toppings using String formatted options.

Example of Formatting with two format items can be found below:

https://devblogs.microsoft.com/scripting/understanding-powershell-and-basic-string-formatting/

Strings have a whole bunch of methods that you can use.

If you want to concatenate you would use the + symbol

for example:

" I love " + $favoriteFood

or

"I love $favoriteFood" 

Task5: Study Different Methods that can be used with Strings. FIND A CHEAT SHEET On the internet and type each String method that you can find on that Sheetsheet.

To be honest, I can’t remember a lot of this stuff so I use a lot of cheat sheets it helps a lot. In my case I gotten hit in the head to many times so I try to work as smart as I can possibly can.

Here is a Cheat-sheet I found on Github.

https://ramblingcookiemonster.github.io/images/Cheat-Sheets/powershell-cheat-sheet.pdf

sources:

sources: https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-04?view=powershell-7.3
https://ss64.com/ps/select-string.html
https://shellgeek.com/powershell-string/
https://www.gngrninja.com/script-ninja/2016/5/22/powershell-getting-started-part-9-all-about-strings
https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/00-introduction?view=powershell-7.3
https://devblogs.microsoft.com/scripting/understanding-powershell-and-basic-string-formatting/

I hope this helps someone out there, Don’t hesitate to reach out to me on Linkedin or Here.

This is all for now!

Peace!!!