Solved

PowerShell Script to Search for PST files

Posted on 2016-10-10
5
35 Views
Last Modified: 2016-11-22
I had this question after viewing Powershell script to find files on network computers..

I am trying to create a script on how to search for pst files in the hard drive. How can I achieve this? Any help on the script would be appreciated.

Thanks
0
Comment
Question by:Junior Sofe
5 Comments
 
LVL 6

Expert Comment

by:Niten Kumar
ID: 41837821
This line of code does the trick, which can be typed directly in PowerShell ISE, in order to avoid script signing:

   1. Start Windows PowerShell ISE
   2. Type gci -path c:\ -recurse -include *.pst|select-object fullname,lastwritetime|export-csv c:\temp\pst.csv and press enter.
    3. Wait until the script finishes

In this case the output is sent to a csv file in the c:\temp folder.


OR you can use the script below:


$strComputers = Get-Content -Path "C:\computernames.txt"
[bool]$firstOutput = $true
foreach($strComputer in $strComputers)
{
$colFiles = Get-Wmiobject -namespace "root\CIMV2" `
-computername $strComputer `
-Query "Select * from CIM_DataFile `
Where Extension = 'pst'"
foreach ($objFile in $colFiles)
{
if($objFile.FileName -ne $null)
{
$filepath = $objFile.Drive + $objFile.Path + $objFile.FileName + "." `
+ $objFile.Extension;
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" `
+ $filepath `
+ "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"

$colOwners = Get-Wmiobject -namespace "root\CIMV2" `
-computername $strComputer `
-Query $query
$objOwner = $colOwners[0]
$user = $objOwner.ReferencedDomainName + "\" + $objOwner.AccountName
$output = $strComputer + "," + $filepath + "," + $user + "," + $objFile.FileSize/1KB + "," + $objFile.LastModified
if($firstOutput)
{
Write-output $output | Out-File -Encoding ascii -filepath "C:\pstdetails.csv"
$firstOutput = $false
}
else
{
Write-output $output | Out-File -Encoding ascii -filepath "C:\pstdetails.csv" -append
}
}
}
}
0
 
LVL 7

Expert Comment

by:Senior IT System Engineer
ID: 41837883
Is it just for one user only or all users in all AD domain ?
0
 

Author Comment

by:Junior Sofe
ID: 41841098
@ ITSystemEngineer...

For a start, its just only for one user.
0
 
LVL 7

Accepted Solution

by:
Senior IT System Engineer earned 500 total points
ID: 41841121
Junior,

use the script below to find multiple extension of '*.PST', '*.OST'

Edit line:
27-31 for the paramater according to your environment
59 for the SMTP address

Function Convert-Size {
	[cmdletbinding()]
	Param (
		[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
		[Alias("Length")]
		[int64]$Size
	)
	Begin {
		If (-Not $ConvertSize) {
			Write-Verbose ("Creating signature from Win32API")
			$Signature =  @"
				[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
				public static extern long StrFormatByteSize( long fileSize, System.Text.StringBuilder buffer, int bufferSize );
"@
			$Global:ConvertSize = Add-Type -Name SizeConverter -MemberDefinition $Signature -PassThru
		}
		Write-Verbose ("Building buffer for string")
		$stringBuilder = New-Object Text.StringBuilder 1024
	}
	Process {
		Write-Verbose ("Converting {0} to upper most size" -f $Size)
		$ConvertSize::StrFormatByteSize( $Size, $stringBuilder, $stringBuilder.Capacity ) | Out-Null
		$stringBuilder.ToString()
	}
}

$ScanPath = "C:\Users\"
$ResultFile = "C:\Temp\Files.csv"
$ResultEmailRecipient = "admin@domain.com"
$ResultEmailSender = "Powershell@localhost.com"
$IncludeFilter = @('*.PST', '*.OST')

#Powershell 3.0
$FileList = Get-ChildItem -Path $ScanPath -Include $IncludeFilter -Recurse -File

# Powershell 2.0
#$FileList = Get-ChildItem -Path $ScanPath -Include $IncludeFilter -Recurse | Where-Object {! $_.PSIsContainer}

$FileList |
	Select-Object FullName, LastWriteTime, @{Name='Size'; Expression={$_.Length | Convert-Size}}, @{Name='ByteSize'; Expression={$_.Length}} |
	Export-Csv -NoTypeInformation -Path $ResultFile

$Summary = $FileList | Group-Object Extension | ForEach-Object {
	$ByteSize = ($_.Group | Measure-Object Length -Sum).Sum
	$_ | Select-Object -Property `
		@{Name='Type'; Expression={$_.Name}},
		@{Name='Size'; Expression={$ByteSize | Convert-Size}},
		@{Name='ByteSize'; Expression={$ByteSize}}
} | Sort-Object -Property ByteSize -Descending

$Body = @"
User File Report for $($ENV:ComputerName):

$($Summary | Format-Table -AutoSize | Out-String)
"@

$Body

Send-MailMessage -Attachments $ResultFile -To $ResultEmailRecipient -From $ResultEmailSender -Subject 'File Report' -Body $Body -SmtpServer smtp.domain.com

Open in new window


Hope that helps and let me know how you go.
0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41897207
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Senior IT System Engineer (https:#a41841121)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

exchangepro
Experts-Exchange Cleanup Volunteer
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
A procedure for exporting installed hotfix details of remote computers using powershell
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now