Powershell strip string

Posted on 2014-03-21
Last Modified: 2014-03-27
Hi guys,


now I need to put this in another variable called VM and exclude the and add * before the MPS-EOP-01 and a start after it.

how can I do this?

that's the script I am working on:

Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer –Credential $cred

Get-Content C:\Reboot_Servers.csv| % { if (!(Test-Connection -ComputerName $_ -Quiet))

$vm=get-vm "*" + $_ + "*"
Restart-VM -VM $VM
Start-Sleep -s 300  # this will wait for 5 minutes


Open in new window

thank you in advance.
Question by:Kay
  • 6
  • 4
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 500 total points
ID: 39945100
$vm=get-vm "*" + $_.split(".")[0] + "*"

Author Comment

ID: 39945207
Excellent Dan!!

Author Comment

ID: 39949979
Dan what does the [0] mean?
LVL 34

Expert Comment

by:Dan Craciun
ID: 39949980
The first element of an array.

Author Comment

ID: 39950974
what exactly is the first element of an array,

so if the server name is

and we use the $_.split(".")[0]

does the first element mean the srv01???

whats the point of that?
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 34

Accepted Solution

Dan Craciun earned 500 total points
ID: 39950999
split() will create an array of strings.

"".split(".") will create this:
[0] srv01
[1] qw
[2] as
[3] dsl
[4] uk

You pick your element using the number. If you wanted dsl you would use

What's the point? Answering your question, I guess :)

Author Comment

ID: 39952746
I am trying to learn powershell :)

Author Comment

ID: 39953836
Dan why doesn't this code work?

dsquery computer -name *qre* | % {$_.split('"=,)} | % {gwmi -ComputerName $_ win32_operatingsystem} | select name 

Open in new window


Author Comment

ID: 39953846
my apologies its working now forget the [2] and ' !
dsquery computer -name *qre* | % {$_.split('"=,')[2]} | % {gwmi -ComputerName $_ win32_operatingsystem} | select name 

Open in new window

LVL 34

Expert Comment

by:Dan Craciun
ID: 39958586
Glad you got it working :)

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 64
Performance Issue Dell / HP Vmware SAP 18 63
Renaming VMware ESXi Hosts 5 63
Microsoft Azure Powershell Book Recommendations 5 20
In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now