Tweak to delete server name RRS feed

  • Question

  • Hello,

    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 ?

    If some one can provide hints then i can modify the script accordingly.

    Thanks .

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

    #Do {

    write-host $Server
    write-host "Searching for the Server $Server in $Path 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”

    Monday, November 18, 2019 11:22 AM


All replies

  • Hi AtulRam

    For a mass treatment use a foreach loop like  the following :

    $Path =C:\pathto\exportFile.txt

    $Servers = Get-Content -Path $Path

    Foreach ($Server in $Servers)


    Do something to $Server




    Monday, November 18, 2019 12:30 PM
  • Hi Olivier,

    thanks for replying.

    can we also get an output like success/failure ?


    Tuesday, November 19, 2019 4:39 AM
  • I added the condition with for loop "Remove-Item -Recurse $Server\$Path"  but i gives me error.

    I think i am missing something with the input.

    can you please suggest. 

    Tuesday, November 19, 2019 8:42 AM
  • Hi AtulRam

    Of course it's possible by using Try... catch, .... finally

    i.e. :



    Do Something

    Write-Host "SUCCESS"

    } # If a error occurs, then exec Catch section


    { Write-Host "FAILED"

    Write-Host "An error occurred: $_.Exception.Message"

    Do something else

    } # If an error occurs, this section runs



    Do something

    Write-Host "The script is done"

    } # this section (optional) runs in every case.



    • Edited by Oliv - TheFrog Tuesday, November 19, 2019 8:50 AM
    • Marked as answer by AtulRam Thursday, November 21, 2019 3:57 PM
    Tuesday, November 19, 2019 8:49 AM
  • thanks a lot.

    I will need more time to integrate your suggestions into the script (noob :D )

    Thursday, November 21, 2019 1:54 PM