We help IT Professionals succeed at work.

writing out put to a file powershell

techdrive
techdrive asked
on
141 Views
Last Modified: 2017-03-30
I have this powershell script and I am trying to get it to write output to a file. It errors and bombs out when I run the script like this. Can anyone tell me what I am missing.


[/$OutFile = $output = "c:\temp\gmembers.txt"
$MyList=get-content "c:\nameofdlgroups.csv" |get-distributiongroup  | select displayname -expandproperty managedby | select displayname,name 

output = `
 foreach($MyItem in $MyList){

 if ($MyprevItem -ne $MyItem.DisplayName){
 $MyItem.DisplayName
 "             $($MyItem.Name)"
 $MyprevItem=$MyItem.DisplayName
 } else{
 "             $($MyItem.Name)"
 } 
 } 
$Output | Out-File $OutFile -Appendcode]





it works running it this way

[code]$MyList=get-content "c:\nameofdlgroups.csv" |get-distributiongroup  | select displayname -expandproperty managedby | select displayname,name 


 foreach($MyItem in $MyList){

 if ($MyprevItem -ne $MyItem.DisplayName){
 $MyItem.DisplayName
 "             $($MyItem.Name)"
 $MyprevItem=$MyItem.DisplayName
 } else{
 "             $($MyItem.Name)"
 } 
 } 

Open in new window

Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Might have been PowerShell 2.0 where it didn't work, ah well, nothing you've said is incorrect footech. I don't think I checked sufficiently well, the problem with writing content like this which I simply never do.

Just one bug for you though.
    $_ = $MyItem

Open in new window

Should be:
    $MyItem = $_

Open in new window

CERTIFIED EXPERT
Top Expert 2014

Commented:
Absolutely correct.  Nice catch!
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
You guys are all outstanding thanks again. I feel so honored to have you guys responding to my question.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.