Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hosts File String Removal - Single Domain
- # Removes a certain line on every Non-Server Domain PC via UNC.
- # Run as Administrator
- # Gets search string to match in hosts file. Sets Hosts file Network Path
- $Pattern = Read-Host -Prompt "Input Hosts File Search String"
- $HostsPath = "\C$\windows\System32\drivers\etc\"
- # Gets Local Domain's Computer List for all OUs. (Could add filter if req)
- $Computers = Get-ADComputer -filter { OperatingSystem -notLike '*Windows Server*' }
- ForEach ( $Computer in $Computers)
- {
- # Adds contents of Text file which match $Pattern to $Content variable
- $Content = Get-Content "\\$($Computer.Name)$($HostsPath)hosts" |
- Select-String -pattern $Pattern
- # Looped If Statement Per PC found in AD - Exits if $Pattern is not found
- if ( $Content -like "*$($Pattern)*")
- {
- # Reads content - Writes all Content but lines containing $Pattern to new File
- Get-Content "\\$($Computer.Name)$($HostsPath)hosts" |
- Select-String -pattern $Pattern -notmatch |
- Out-File "\\$($Computer.Name)$($HostsPath)hosts2"
- # Renames original hosts file, then renames our new file.
- Rename-Item -path "\\$($Computer.Name)$($HostsPath)hosts" `
- -newname "hosts-BAK-$((Get-Date).ToString('MM-dd-yyyy-hh-mm'))"
- Rename-Item -path "\\$($Computer.Name)$($HostsPath)hosts2" -newname "hosts"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment