Redirecting Powershell Output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
EddieJRainer
Posts: 3
Joined: 08 Aug 2016 11:07

Redirecting Powershell Output

#1 Post by EddieJRainer » 08 Aug 2016 11:32

Hey all,

I found this neat little powershell script, https://gallery.technet.microsoft.com/scriptcenter/Get-The-Name-of-Installed-b10fd073/view/Discussions#content, which basically displays what anti-virus is being used. I'm not gonna show the entire code, it's very long and you can visit the website if you want.

Anyways, I tried creating a batch file with this code:

Code: Select all

PowerShell.exe -executionpolicy Unrestricted -File "C:\getps1.ps1" >> gettxt.txt


In theory, this should redirect the output of the powershell to the gettxt.txt file, however, it doesn't work in practice. Any ideas?

Thank you.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Redirecting Powershell Output

#2 Post by Squashman » 08 Aug 2016 13:12

Works for me.

Are you trying to write the output to the root of the system drive?

EddieJRainer
Posts: 3
Joined: 08 Aug 2016 11:07

Re: Redirecting Powershell Output

#3 Post by EddieJRainer » 08 Aug 2016 13:47

Squashman wrote:Works for me.

Are you trying to write the output to the root of the system drive?


I'm trying to record the name of the AV to the "gettxt.txt" file. In my case, the AV is Windows Defender. When the script executes, I want the output, in this case "Windows Defender", to be written to the text file. The program runs, but no output is written to the text file.

When you said it worked for you, were you operating in the C:\ drive?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Redirecting Powershell Output

#4 Post by aGerman » 08 Aug 2016 14:04

Users have restricted permissions to write in the C:\ root folder. Is there any specific reason why you can't run it in a subfolder?

Regards
aGerman

EddieJRainer
Posts: 3
Joined: 08 Aug 2016 11:07

[SOLVED] Redirecting Powershell Output

#5 Post by EddieJRainer » 08 Aug 2016 14:20

aGerman wrote:Users have restricted permissions to write in the C:\ root folder. Is there any specific reason why you can't run it in a subfolder?

Regards
aGerman


Yes, running it in a subfolder works, It's just easier to run in the c:\ drive because I don't have to exactly know what the user name is.

But, what you suggested works. If you know of a way to write to the C:\ drive, it would be great, but if not, it's fine.

Thank you.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: [SOLVED] Redirecting Powershell Output

#6 Post by Squashman » 08 Aug 2016 14:23

EddieJRainer wrote:
aGerman wrote:Users have restricted permissions to write in the C:\ root folder. Is there any specific reason why you can't run it in a subfolder?

Regards
aGerman


Yes, running it in a subfolder works, It's just easier to run in the c:\ drive because I don't have to exactly know what the user name is.

But, what you suggested works. If you know of a way to write to the C:\ drive, it would be great, but if not, it's fine.

Thank you.

The username is an environmental variable. Open up a cmd prompt and type: SET

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [SOLVED] Redirecting Powershell Output

#7 Post by aGerman » 08 Aug 2016 14:31

EddieJRainer wrote:It's just easier to run in the c:\ drive because I don't have to exactly know what the user name is.
See %userprofile%.

EddieJRainer wrote:If you know of a way to write to the C:\ drive, it would be great, but if not, it's fine.
You should be able to create a subfolder in C:\ using MD.

Regards
aGerman

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Redirecting Powershell Output

#8 Post by Compo » 09 Aug 2016 03:57

You should be able to do it without powershell too.
Antivirus products can put their details in any one of two locations hence the two lines.

Code: Select all

@Echo Off
SetLocal
(Set _R=%UserProfile%\Desktop\gettxt.txt)
(Set _N=/NAMESPACE:\\root\SecurityCenter)
>"%_R%" (
   WMIc %_N% PATH AntiVirusProduct GET displayName
   WMIc %_N%2 PATH AntiVirusProduct GET displayName
)
I cannot test since I've not used an antivirus type product for more than a decade.

Post Reply