modify text string in file

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Rob Jaudon Rob Jaudon 1 year, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #25092
    Profile photo of Rob Jaudon
    Rob Jaudon


    I am looking for direction on manipulating text in a text file. I have a bunch on entries for device names in a file and I would like to generate a pwd for each machine that is the reverse of the device name. This is for a testing lab and nothing more. I don't know where to start for taking an input string and reverse ordering it to out it back onto the file.

    Process for script

    1. Read a text file for the name
    2. Reverse the output to console or into a tab delimited output


    DeviceName1 1emanecived
    DeviceName2 2emanecived

    Profile photo of Simon Wåhlin
    Simon Wåhlin

    Try this:

    Get-Content -Path 'C:\folder\devices.txt' | Foreach{-join$_[-1..-[$_.length]]} | Out-File -Path 'C:\folder\passwords.txt'

    Here is an explanation:
    You can get one char of a string by using the index of the char, like this:

    $String = 'test'

    This will output only the first char of $String (Index 0)

    If you want to select a range of chars, specify an index range:


    This will ouput 'tes', the range of chars from index 0 to index 2

    PowerShell also accepts negative indexes, for example:


    This will return the last char of $String

    To turn a string backwards, simply index the range from the last char to the first char.
    Since $String in this example has a length of 4, this will return the string backwards:


    To make it more dynamic, replace -4 with -($String.length), hence:



    Profile photo of Itta

    There may be other simple ways to solve this, however below is my version. Note: *You need to loop the below code for all array elements*

    $A = Get-Content D:\devices.txt
    $Split = $A[0] -split ""
    $rev = $Split -join "
    $revString = $Rev.ToLower()
    $HT = [ordered]@{'DeviceName' = $A[0]; 'Password' = $revString}
    $Obj = New-Object -TypeName psobject -Property $HT
    $Obj | ft -AutoSize

    Profile photo of Rob Jaudon
    Rob Jaudon


    Thank you for the explanation. That is a HUGE help.


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.