# modify text string in file

**URL:** <https://forums.powershell.org/t/modify-text-string-in-file/4172>\
**Category:** PowerShell Help\
**Created:** [May 11, 2015, 2:36am UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172 "2015-05-11T02:36:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob-jaudon-2](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@rob-jaudon-2](https://forums.powershell.org/u/rob-jaudon-2)\
**Post date:** [May 11, 2015, 2:36am UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172/1 "2015-05-11T02:36:03Z")

</div>

All,

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

EX

DeviceName1 1emanecived  
DeviceName2 2emanecived

---

<div class="post-metadata">

**Author:** ![simon-wahlin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/simon-wahlin/32/407_2.png) [@simon-wahlin](https://forums.powershell.org/u/simon-wahlin)\
**Post date:** [May 11, 2015, 7:19am UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172/2 "2015-05-11T07:19:38Z")

</div>

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'
$String[0]
```

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

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

```
$String[0..2]
```

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

PowerShell also accepts negative indexes, for example:

```
$String[-1]
```

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:

```
$String[-1..-4]
```

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

```
$String[-1..-[$String.length]]
```

/Simon

---

<div class="post-metadata">

**Author:** ![prabhas-itta](https://avatars.discourse-cdn.com/v4/letter/p/b38774/32.png) [@prabhas-itta](https://forums.powershell.org/u/prabhas-itta)\
**Post date:** [May 11, 2015, 11:08pm UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172/3 "2015-05-11T23:08:55Z")

</div>

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 “”  
[array]::Reverse($Split)  
$rev = $Split -join ‘’  
$revString = $Rev.ToLower()  
$HT = [ordered]@{‘DeviceName’ = $A[0]; ‘Password’ = $revString}  
$Obj = New-Object -TypeName psobject -Property $HT  
$Obj | ft -AutoSize

---

<div class="post-metadata">

**Author:** ![rob-jaudon-2](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@rob-jaudon-2](https://forums.powershell.org/u/rob-jaudon-2)\
**Post date:** [May 21, 2015, 4:19am UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172/4 "2015-05-21T04:19:35Z")

</div>

Simon,

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

Rob

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/modify-text-string-in-file/4172/5 "2024-05-16T20:45:14Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


