Add static route from text file

Welcome Forums General PowerShell Q&A Add static route from text file

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

11 months, 3 weeks ago.

  • Author
  • #89839

    Points: 1
    Rank: Member


    I need to transfer static routes from server to server .
    If I get the old server routes by
    Get-NetRoute |where {$_.RouteMetric -eq '1'} | FT DestinationPrefix,NextHop,RouteMetric | out-file c:\newroutes.txt

    is this possible to add them to the new server by using New-NetRoute from the text file ?

    or course any other way to make this migration is welcome .


  • #89842

    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    First, if you're looking to transfer data, text is a terrible format. Don't use Format-Table – it discards there structure of the data. Just Export-CliXML. That maintains the structure of the data – the properties of the objects. You can Import-CliXML them to recreate the objects.

    Text is lovely on Linux. Well, not really, but try to avoid text files as a means of data storage. describes which parameters New-NetRoute can accept from the pipeline (e.g., "Import-CliXML mydata.xml | New-NetRoute"). If you need it to use properties which don't accept pipeline input, then you'll have to ForEach it.

    Import-CliXML whatever.xml |
    ForEach-Object {
    New-NetRoute -Parameter $_.Property -OtherParam $_.OtherProp

    Within ForEach, $_ lets you access one object at a time from your CliXML file, so you can refer to the individual properties like RouteMetric and such.

    • #89995

      Points: 1
      Rank: Member

      Thanks Don

      On source server :
      Get-NetRoute |where {$_.RouteMetric -eq '1'} | export-CliXML c:\newroutes.xml

      On Destination server :
      Import-CliXML c:\newroutes.xml |
      ForEach-Object {
      New-NetRoute -DestinationPrefix $_.DestinationPrefix -NextHop $_.NextHop -RouteMetric $_.RouteMetric -InterfaceAlias $_.InterfaceAlias

      Did the job

The topic ‘Add static route from text file’ is closed to new replies.