Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
995 views
in PowerShell by 66 69 85

How to Split String into multi strings using PowerShell?

I have the following string

$txt = "www.google.com"

I want to extract google word from the URL as below

google

what is the best way to perform split in PowerShell?


1 Answer

0 like 0 dislike
by 66 69 85

You can extract specific text using split() method in PowerShell.

$txt = "www.google.com"
$txt.Split(".")

Split() return an Array and the first element is www. So if you want to return google write the following:

$y = $txt.Split(".")[1]

Another example

$email = "mail@gmail.com"
$x = $email.Split("@")
$domain = $x[1]
$domain = $domain.Split(".")[0]

Write-Host "email: " $email
Write-Host "Domain: " $domain

Output:

email: mail@gmail.com
Domain: gmail
If you don’t ask, the answer is always NO!
...