How to take output in a file for Script

I Want to take output in a file for the below script…

$computer = get-content C:\DNS.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer |where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
}

you can pipe it to the cmdlet out-file. i.e

$computer = get-content C:\DNS.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer |where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
} | out-file c:\temp\DNS-Results.txt

Hi Graham Beer, Am getting error as an empty pipe element is not allowed…

Use the pipeline like this.

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "ipenabled = 'True'" -ComputerName $computer
$NICs | ForEach-Object {
    $DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
    $_.SetDNSServerSearchOrder($DNSServers)
    $_.SetDynamicDNSRegistration(“TRUE”)} | out-file .\NICs.txt -Append
 $DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”

You will need to change the quotes back to standard text double quotes.

 $DNSServers = "0.0.0.0","0.0.0.0","0.0.0.0","0.0.0.0"

Still am getting same error as “an empty pipe element is not allowed” with Line details

You can use the variable to store the results then pipe those results to a file.

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter “ipenabled = ‘True’” -ComputerName localhost
$result = $NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$.SetDNSServerSearchOrder($DNSServers)
$
.SetDynamicDNSRegistration(“TRUE”)

$result | out-file c:\dns.txt -append

You can wrap the foreach loop in a subexpression

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter “ipenabled = ‘True’” -ComputerName localhost
$($NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$.SetDNSServerSearchOrder($DNSServers)
$
.SetDynamicDNSRegistration(“TRUE”)}) | out-file .\NICs.txt -Append

Or you can try like below

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter “ipenabled = ‘True’” -ComputerName localhost
$result = @()
$NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$.SetDNSServerSearchOrder($DNSServers)
$
.SetDynamicDNSRegistration(“TRUE”)
$result += $_
}

$result | out-file c:\dns.txt -append