Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Tweak to delete server name

Tweak to delete server name

Tweak to delete server name


I have below script to input server host name or IP which returns the text files for those host names/IP as output.

Then i have to manually delete those host names from the text files.

How can I tweak the script to directly delete that host name from text file and save it ? Also instead of one host name as input can we put down a list of servers to search and delete with success/failure log ?

If someone can suggest how to achieve this.

Thanks .

[string]$Server = Read-Host -Prompt 'Input the server name to search and hit Enter'
#[string]$Path1 = \\servername.com\d$
[string]$Path = "\\servername.com\d$"

#Do {

write-host $Server
write-host "Searching for the Server $Server in $Path .....search is in progress.. `n `n" -foregroundcolor green

$PathArray = @()

$Time = (Measure-Command {

Get-ChildItem $Path -recurse -Filter "*.txt" | Where-Object {$_.Name -notmatch "backup","Notes","test","copy"} |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Server) {
$PathArray += $_.FullName


Write-Host "$Server is located in the following paths : " -foregroundcolor magenta
$PathArray | ForEach-Object {$_}
if (!$PathArray) { Write-Host "Server does not exist in the selected path. Please search in other locations. `n" -foregroundcolor Red }
#write-host $PathArray
#write-host "`n Time taken to search is : $time minutes. " -foregroundcolor green

#Read-Host -Prompt “Press Enter to exit”

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close