0

Please help me to export the output of the script as a CSV.

$siteUrl = "https://webapplication"
$daysThreshold = 30  # Number of days to consider as "active"

# Load SharePoint client libraries
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Connect to the site
$web = Get-SPWeb $siteUrl

# Get the user information list
$userInfoList = $web.SiteUserInfoList

# Define the date threshold
$thresholdDate = (Get-Date).AddDays(-$daysThreshold)

# Query the list to get active users
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Query = "<Where><Geq><FieldRef Name='Last_x0020_Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>$($thresholdDate.ToString("yyyy-MM-ddTHH:mm:ssZ"))</Value></Geq></Where>"

$items = $userInfoList.GetItems($query)
foreach ($item in $items) {
    Write-Host "User: $($item['Name'])"
}

# Clean up
$web.Dispose()

1 Answer 1

1

This is not really related to SharePoint but more of a programming question that would be more suited for Stack Overflow.

$siteUrl = "https://webapplication"
$daysThreshold = 30  # Number of days to consider as "active"

# Load SharePoint client libraries
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Connect to the site
$web = Get-SPWeb $siteUrl

# Get the user information list
$userInfoList = $web.SiteUserInfoList

# Define the date threshold
$thresholdDate = (Get-Date).AddDays(-$daysThreshold)

# Query the list to get active users
$query = New-Object Microsoft.SharePoint.SPQuery
$query.Query = "<Where><Geq><FieldRef Name='Last_x0020_Modified' /><Value IncludeTimeValue='TRUE' Type='DateTime'>$($thresholdDate.ToString("yyyy-MM-ddTHH:mm:ssZ"))</Value></Geq></Where>"

# Create a array to store all objects in
$arr = [System.Collections.Generic.List[PSObject]]::new()

$items = $userInfoList.GetItems($query)
foreach ($item in $items) {
    Write-Host "User: $($item['Name'])"
    $arr.add(
        [pscustomobject]@{
            "Name" = $item['Name']
        }
    )
}

# Clean up
$web.Dispose()

# Export to a CSV
$arr | Export-Csv -Path "C:\Temp\users.csv" -NoTypeInformation

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.