# Remove line from text file

**URL:** <https://forums.powershell.org/t/remove-line-from-text-file/1720>\
**Category:** PowerShell Help\
**Created:** [July 11, 2013, 6:25am UTC](https://forums.powershell.org/t/remove-line-from-text-file/1720 "2013-07-11T06:25:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek-engel](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@derek-engel](https://forums.powershell.org/u/derek-engel)\
**Post date:** [July 11, 2013, 6:25am UTC](https://forums.powershell.org/t/remove-line-from-text-file/1720/1 "2013-07-11T06:25:28Z")

</div>

I have a text file located at c:\computers.txt. Â Inside this file is the following:

computer1

computer 345

computername

computer 7 8 8

I want to delete a line from this file. Â The trick is, to delete only what I enter into a variable. Â So if $deletecomputer = ‘computer 345’ I want to delete just computer 345. Â The variable would could contain any one computername.

Everything I’ve found shows how to replace text. Â I’m not necessarily looking to replace. Â I’m looking to delete.

---

<div class="post-metadata">

**Author:** ![artb0514](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@artb0514](https://forums.powershell.org/u/artb0514)\
**Post date:** [July 11, 2013, 6:32am UTC](https://forums.powershell.org/t/remove-line-from-text-file/1720/2 "2013-07-11T06:32:06Z")

</div>

```
$Computers = Get-Content C:\computers.txt
$del = Read-Host -Prompt "Enter Computer name to delete"
$Computers = $Computers | Where {$_ -ne $del}
$Computers | Out-File C:\computers.txt -Force
```

---

<div class="post-metadata">

**Author:** ![derek-engel](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@derek-engel](https://forums.powershell.org/u/derek-engel)\
**Post date:** [July 11, 2013, 7:22am UTC](https://forums.powershell.org/t/remove-line-from-text-file/1720/3 "2013-07-11T07:22:57Z")

</div>

> [@8394](#):
>
> \<pre class=“d4pbbc-pre”\>$Computers = Get-Content C:\computers.txt  
> $del = Read-Host -Prompt â€œEnter Computer name to deleteâ€  
> $Computers = $Computers | Where {$\_ -ne $del}  
> $Computers | Out-File C:\computers.txt -Force

Thank you.

---

<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:57pm UTC](https://forums.powershell.org/t/remove-line-from-text-file/1720/4 "2024-05-16T20:57:41Z")

</div>


