indexOf replace in file

Welcome Forums General PowerShell Q&A indexOf replace in file

This topic contains 1 reply, has 2 voices, and was last updated by

1 month, 2 weeks ago.

  • Author
  • #169063

    Topics: 17
    Replies: 10
    Points: 90
    Rank: Member

    I have a list of SQL Servers in a .csv file. Some of the SQL Servers are named instances and look like this. I need to strip the slash and everything after it. This snippet does that but I am not sure how to put it in a script:

    $pos = "MySQLServer001\NamedInstance".IndexOf("\")

    The result is MySQLServer001

    Not sure where to go from here:

    $startingFile = "c:\temp\myServers.csv"
    $endingFile = "c:\temp\myServers_NoSlashes.csv"


  • #169066

    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    There'd be an easier way I think:

    'MySQLServer001\NamedInstance' -replace '\\.*'

    You should carefully read the help for Import-Csv and Export-Csv and Foreach-Object or Select-Object (calculated properties). You should read the complete help including the examples.

You must be logged in to reply to this topic.