Solved

Powershell: Export-Csv gives only one result

Posted on 2014-03-31
3
800 Views
Last Modified: 2014-03-31
Hi,

I have this script that shows the shares that are published bij DFS.
I want to pipe the output to a csv file. There are more than 40 shares in DFS, but the script places only one result into the csv.
Only if I add an "-append"at the of the Export-Csv line, I get all the results.
The problem to that is, the csv is not overwritten.
Can someone help me on this ?

Clear-Host
$search= "department"
$dfsroots=Get-DfsnRoot
foreach ($root in $dfsroots) {
write-Host $root.NameSpacePath
$check=Get-DfsnFolder -Path "$($root.NamespacePath)\*" | Get-DfsnFolderTarget | Select-String $search foreach ($path in $check) {
$path=$path.ToString().Replace('?','$') -split "="
$path=$path.Split(',')
$shares=$path[3].Replace(')','')
#Out-File -filepath "C:\Temp\Shares.txt" -InputObject "$shares"
""|select-object @{Name="Shares";Expression={$shares}}|Export-Csv C:\Temp\shares.csv -NoTypeInformation -append 
}
}

Open in new window

0
Comment
Question by:Loyall
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39966640
The script behaves exactly as it should.
In the loop you get exactly one share, and you export that to the shares.csv file.

If you want to clear the file, just put this at the top:
Clear-Content C:\Temp\shares.csv

Open in new window

HTH,
Dan
0
 
LVL 2

Author Closing Comment

by:Loyall
ID: 39968500
Thanks a lot, Dan !
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39968550
Glad I could help!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
powershell query 2 24
XML Removal- Powershell 4 25
CSV file manipulation 6 26
What is Microsoft.Exchange.WebServices.Data.ExchangeService in PowerShell? 4 26
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question