powershell extract string

bibi92
bibi92 used Ask the Experts™
on
Hello,

In a script, I execute :

$sql=(Get-ItemProperty "$fullpath\Setup").SQLDataRoot
echo $sql :
Z:\mssql\INST1\MSSQL10_50.INST1\MSSQL\data\MSSQL10_50.INST1\MSSQL
How can I extract Z:\mssql\INST1?

Thanks

Regards

bibi
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Infrastructure Architect
Commented:
Try..
$String = "Z:\mssql\INST1\MSSQL10_50.INST1\MSSQL\data\MSSQL10_50.INST1\MSSQL"
($String.Split("\") |  Select -first 3) -Join("\")

Open in new window

Meir RivkinFull stack Software Engineer

Commented:
(Get-ItemProperty "$fullpath\Setup").SQLDataRoot.Split("\")[0..2] -join ("\")

Open in new window

Author

Commented:
Thanks bibi

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial