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
in PowerShell by 5 20 37

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


what is the best way to perform split in PowerShell?

1 Answer

0 like 0 dislike
by 5 20 37

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

$txt = "www.google.com"

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


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