Add "Create Date" and "Modified Date"

Hi,

I found this powershell script online. Works fine, but how can it be edited so that the “Create Date” and “Modified Date” shows as well in the results (scv file).

Many Thanks,

Adam

Add-Type -Path “C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll”

Add-Type -Path “C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll”

$now=Get-Date -format “dd-MMM-yy,HH:mm:ss”

$fileFormat = Get-Date -format “dd-MMM-yy_HHmmss”

Write-Host “Script Start : ‘$($now)’” -ForegroundColor Yellow

$global:SourceCount = 0 ### To know the total count of the documents to be processed

$global:Processed = 0

$global:OutFilePath = “C:\Reports\files_” + $fileFormat + “.csv”

$header = “Date,Time,Type,Parent,Name,Path,FilesCount,FileSize(bytes),Remark”

Add-Content -Path $global:OutFilePath -Value “`n $header”

$username = “abc@blabla.com

$password = “12345”

$srcUrl = “https://blabla.sharepoint.com/sites/bla” ### https://domain/sites/<sitename>

$srcLibrary = “Team”

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

function to create a log for the report in csv

function WriteLog

{

param (

[Parameter(Mandatory=$true)] $type, $folderName,$name,$path,$fileCount,$fileSize,$remark

)

$nowTime=Get-Date -format “dd-MMM-yy,HH:mm:ss”

$folderName = $folderName.replace(",","|") ### sometime folder / file name has comma so replace it with something

$name = $name.replace(",","|")

$path = $path.replace(",","|")

$lineContent = “$($nowTime),$($type),$($folderName),$($name),$($path),$($fileCount),$($fileSize),$($remark)”

Add-Content -Path $global:OutFilePath -Value “$lineContent”

$global:Processed = $global:Processed +1

}

function ScanFolders

{

param (

[Parameter(Mandatory=$true)] $srcfolder, $parentName

)

$remarkDetail = “”

$replacedUser=""

Write-Host “Total Count: $($global:SourceCount) Completed: $($global:Processed)” -ForegroundColor Cyan

Write-Host "Navigate to: " $srcfolder.ServerRelativeUrl -ForegroundColor Yellow

$folderItem = $srcfolder.ListItemAllFields

#$srcContext.Load($f)

$srcContext.Load($folderItem)

$srcContext.ExecuteQuery()

$authorEmail = $folderItem[“Author”].Email

$editorEmail = $folderItem[“Editor”].Email

$filepath = $folderItem[“FileDirRef”]

#$fileSize = $fItem[“File_x0020_Size”]

$fileName = $srcfolder.Name

$fileCol = $srcfolder.Files

$srcContext.Load($fileCol)

$srcContext.ExecuteQuery()

WriteLog “Folder” $parentName $fileName $filepath $fileCol.Count 0 $remarkDetail

foreach ($f in $fileCol)

{

$remarkDetail = “”

$replacedUser=""

$fItem = $f.ListItemAllFields

#$srcContext.Load($f)

$srcContext.Load($fItem)

$srcContext.ExecuteQuery()

$authorEmail = $fItem[“Author”].Email

$editorEmail = $fItem[“Editor”].Email

$filepath = $fItem[“FileDirRef”]

$fileSize = $fItem[“File_x0020_Size”]

$fileName = $fItem[“FileLeafRef”]

$fileCreated=$Fitem[“Created”]

WriteLog “File” $srcfolder.Name $fileName $filepath 0 $fileSize $remarkDetail

}

$fL1FolderColl = $srcfolder.Folders

$srcContext.Load($fL1FolderColl);

$srcContext.ExecuteQuery();

foreach ($myFolder in $fL1FolderColl)

{

$srcContext.Load($myFolder)

$srcContext.ExecuteQuery()

ScanFolders $myFolder $srcfolder.Name

}

}

The script starts here to run

Write-Host “Authenticating …” -ForegroundColor White

$srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl)

$srcContext.Credentials = $credentials

$srcWeb = $srcContext.Web

$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)

$query = New-Object Microsoft.SharePoint.Client.CamlQuery

$listItems = $srcList.GetItems($query)

$srcContext.Load($srcList)

$srcContext.Load($listItems)

$srcContext.ExecuteQuery()

$global:SourceCount = $srcList.ItemCount

Write-Host “Total Count: $($global:SourceCount)” -ForegroundColor Cyan

foreach($item in $listItems)

{

if($item.FileSystemObjectType -eq “File”)

{

$remarkDetail = “”

$replacedUser=""

$srcF = $item.File

$fItem = $srcF.ListItemAllFields

$srcContext.Load($srcF)

$srcContext.Load($fItem)

$srcContext.ExecuteQuery()

$authorEmail = $fItem[“Author”].Email

$editorEmail = $fItem[“Editor”].Email

$filepath = $fItem[“FileDirRef”]

$fileSize = $fItem[“File_x0020_Size”]

$fileName = $fItem[“FileLeafRef”]

WriteLog “File” “Root” $fileName $filepath 0 $fileSize $remarkDetail

}

elseif ($item.FileSystemObjectType -eq “Folder”)

{

$srcContext.Load($item)

$srcContext.ExecuteQuery()

$folder = $srcWeb.GetFolderByServerRelativeUrl($item.FieldValues[“FileRef”].ToString())

$srcContext.Load($folder)

$srcContext.ExecuteQuery()

ScanFolders $folder “Root”

}

}

$now=Get-Date -format “dd-MMM-yy,HH:mm:ss”

Write-Host “Total Count: $($global:SourceCount) Completed: $($global:Processed)” -ForegroundColor Cyan

Write-Host “END Start : ‘$($now)’” -ForegroundColor Yellow

If I’m honest, I’d probably re-write the script to use Export-CSV or similar, as it seems to be re-writing the wheel a bit there, with the risk that the CSV is malformed. In fact, there seems to be headers for both Date and Time separately, whilst only writing a single DateTime column later.

 

 

As for this…

can it be edited so that the "Create Date" and "Modified Date" shows as well in the results (scv file).

It’s you code/your file, add/remove whatever you like. You just need to provide that information in that WriteLog function.
Yet, ditto to what James Ruskin, just said.

Personally, I’d take this as an example and re-write the entire thing to some more succinct. Meaning, if no one is going to be watching this script run, then all the Write-Host stuff is moot. You can just use the *-Transcript cmdlets to capture a log on what happened, or write to log file directly and email the log to yourself or whomever. As well as dealing with the variable scope implementation. https://www.sapien.com/blog/2013/03/06/first-rule-of-powershell-scoping-rules