0

Anyone would help to figure out how to remove the last few characters in PowerShell, for example:

server\instance

need to remove \instance part to become:

server
1
  • Do you have anything you have already tried yet? Using a string split on the slash and grabbing the first portion of the array is where I might go with it. Commented Jun 9, 2016 at 15:46

4 Answers 4

1

I'm not sure I completely understand, but if service\instance is a string you could do it with

"server\instance" -replace "\\.*", ""

to replace everything \ and after with an empty string.

Sign up to request clarification or add additional context in comments.

Comments

1

One option would be to use Split-Path

Example

Split-Path 'server\instance'

Result

server

1 Comment

This is actually the "best" awnser since the builtin cmdlet split-path is made for this by microsoft.
0

Use Substring like this:

$s = 'server\instance'
$s.Substring(0, $s.IndexOf('\'))

Comments

0

Also:

PS C:\> ('server\instance' -split '\\')[0]
server

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.