Welcome Forums General PowerShell Q&A indexOf replace in file

Viewing 1 reply thread
  • Author
    • #169063
      Topics: 18
      Replies: 10
      Points: 102
      Rank: Participant

      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: 2
      Replies: 1736
      Points: 3,551
      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.

Viewing 1 reply thread
  • The topic ‘indexOf replace in file’ is closed to new replies.