2

I am writing a PowerShell script to gather general information on our servers. I wrote the script so that it outputs to a file called output.txt via PowerShells Start-Transcript cmdlet. Output works fine. However I just want the output in the file and not displayed on the console.

I have been looking and attempting to see if Start-Transcription can suppress the console output but I have not found anything.

This is a very cut down version of the code I am using-

Start-Transcript -path "Path\To\Output\File.txt"
$servers = Get-Content -path "Path\To\Servers\List\file.txt"
foreach ($server in $servers)
{
net view
net use
net session
}
Stop-Transcript

It all outputs to the file correctly but I just would like it to NOT display the script/command results in the console. If that is possible.

3
  • 1
    the Start-Transcript cmdlet is not designed to suppress output to the screen. [grin] if you want things quiet, then assign them to a $Var. then, instead of the transcript cmdlets, simply save it to a file directly. Commented Jan 29, 2019 at 18:37
  • @Lee_Dailey any advice on how to do this? I'm only dabble in PowerShell and programming in general so a lot of this is foreign territory to me. Commented Jan 29, 2019 at 18:50
  • it looks like ErikE has posted an answer that otta work. my personal pref would be to assign the output of each exe to a $Var and then send that to a file with Add-Content, but that is a personal pref. ///// as an aside, have you tried using the built in powershell cmdlets to get the info as objects instead of raw, semi-structured text? try looking at Get-Help *net* for some ideas. Commented Jan 29, 2019 at 19:13

1 Answer 1

1

Would this work?

$FilePath = 'Path\To\Output\File.txt'

net view | Out-File -FilePath $FilePath
net use | Out-File -FilePath $FilePath -NoClobber -Append
net session | Out-File -FilePath $FilePath -NoClobber -Append

Or bundle it:

Invoke-Command {net view ; net use ; net session} | Out-File -FilePath $FilePath -NoClobber -Append

EDIT based on comment (but written freely from memory on an iphone so maybe minor mistakes): To run this remotely against a list of servers you first enable Powershell remoting on the servers, many ways to do it and here is one to run in a local powershell session on each server (Runas Admin):

winrm quickconfig

Then, assuming they all have the same login, you can:

$Cred = Get-Credential
$Servers = ’server1.example.com’,’server2.example.com’

Invoke-Command -ComputerNames $Servers -Credential $Cred -ScriptBlock {
 Do stuff
 Do some other stuff
} | Out-File -FilePath $FilePath -NoClobber -Append

Results are returned as an array so if you want to separate the output per server you could try:

$a = Invoke-Command [...]etc but skip the |Out-File

then do some loop which in essence does this part, giving you the manual way here:

$a[0] | Out-File -FilePath $FilePath1 -NoClobber -Append #result from first computer
$a[1] | Out-File -FilePath $FilePath2 -NoClobber -Append #result from second computer
...

and an example loop:

$a | Foreach-Object {$_ | Out-File -FilePath $Path -Append -NoClobber}

And to read the servernames from a text file, one servername per line:

$Servers = Get-Content -Path ’C:\temp\example.txt’
Invoke-Command -ComputerName $Servers [...]etc
Sign up to request clarification or add additional context in comments.

2 Comments

I edited the code above to include some critical parts that I forgot. I ran the bundled code it piped back with the first server and then threw an error: ,"The list of servers is not available for this workgroup".
Can't edit my old comment so I am just adding a new. I was a dunce and did not add the \\127.0.0.1\ to the net view command. But this works! However I think this only does it to the local machine. I was hoping I could run this against a text file of servers. How would I go about doing that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.