modify text string in file


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

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:



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


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