简体   繁体   中英

Export to CSV using PowerShell

I'm using a PowerShell script to grab the root site URL and all the subsites within the SharePoint site and outputting the URL, title and total users as shown below.

Url: http://sourcevideo.f.com | Title: The Source Video | Users: 3345
Url: http://sourcevideo.f.com/AGap | Title: A Gap | Users: 3345
Url: http://sourcevideo.f.com/AVideos | Title: Videos | Users: 417
Url: http://sourcevideo.f.com/BCt | Title: BC Japan | Users: 39

How would I be able to export that to a CSV with three columns?

Code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteCollections = Get-SPWebApplication "http://sourcevideo.f.com" |
                   Get-SPSite -Limit All
foreach ($Site in $SiteCollections) {
    foreach ($Web in $Site.AllWebs) {
        Write-Host "Url:"$web.URL "| Title:"$web.Title "| Users:" $web.AllUsers.Count
    }
    Write-Host ""
}

To expand on Lee's answer, the code would look like this:

$WebInfo = @(
“$web.url”
“$web.title”
"$web.allusers.count"
)
$properties = @{'website url'=$WebInfo[0];'website title'=$WebInfo[1];'user count'=$WebInfo[2]}
$WebsiteObject = New-Object -TypeName PSObject -Property $properties
$WebsiteObject | Export-Csv -path "put path here" -NoTypeInformation 

I have not tested this.

I think what you need is to create a new object

New-Object PSObject -Property @{Url=$web.URL;Title=$web.Title;Users=$web.AllUsers.Count}

Here is a completed script i wrote but can test as i dont have the sharepoint plugin

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteCollections = Get-SPWebApplication "http://sourcevideo.f.com" | Get-SPSite -Limit All | %{
    $Site = $_
    $Site | %{
        New-Object PSObject -Property @{Url=$_.URL;Title=$_.Title;Users=$_.AllUsers.Count}
    }
} | export-csv -Path C:\Test\Output.csv -NoTypeInformation

Assign the outer foreach to a variable and
in the inner one build a [PSCustomObject]

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SiteCollections = Get-SPWebApplication "http://sourcevideo.f.com" |
                   Get-SPSite -Limit All
$Data = foreach ($Site in $SiteCollections) {
    foreach ($Web in $Site.AllWebs) {
        [PSCustomObject]@{
            Url   = $web.URL
            Title = $web.Title
            Users = $web.AllUsers.Count
        }
    }
}
$Data | Export-Csv .\yourName.csv -NoTypeInformation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM