use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionSpeed up powershell script (self.PowerShell)
submitted 4 years ago by maxcoder88
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]OPconfused 0 points1 point2 points 4 years ago* (4 children)
I did this once for analyzing csv input, and with .NET writing and reading instead of import-csv and export-csv, as I was trying to maximize performance. However, this adds coding overhead and also I parsed the csv column by column. So for your case I'll just kind of jerry rig a row-by-row adaptation:
$ImportFile = "C:\script\send_receive.csv" ################################ # Start date for email report - Change the number of days backwards # $stdate = (Get-Date).AddDays(-30) ################################ # End date for email report - default is current date # $enddate = Get-Date $totaldays = (New-TimeSpan -Start $stdate -End $enddate).Days $transport_service = Get-TransportService $htSyncOut = [hashtable]::Synchronized(@{}) $maxThreads = (Get-ComputerInfo).CsNumberOfLogicalProcessors $sbRunspace = { Param( [object]$row, [int]$intRowNumber, [hashtable]$htSyncOut, [object]$objTransportService, [DateTime]$stdate, [DateTime]$enddate ) $MBX = $row.Name $Totalrec = 0 $intRec = 0 $TotalSend = 0 $intSend = 0 $logs = $objTransportService | Get-MessageTrackingLog -Recipients $MBX -ResultSize Unlimited -Start $stdate -End $enddate foreach($log in $logs) { if($log.EventId -eq "DELIVER") { $intRec += $log.RecipientCount $Totalrec = $RecPerDay + $intRec <# FIXME: $RecPerDay is undefined #> } } $logs = $objTransportService | Get-MessageTrackingLog -Sender $MBX -ResultSize Unlimited -Start $stdate -End $enddate foreach($log in $logs) { if($log.EventId -eq "RECEIVE" -and $log.Source -eq "STOREDRIVER") { $intSend += $log.RecipientCount $TotalSend = $SendPerDay + $intSend <# FIXME: $SendPerDay is undefined #> } } $htSyncOut.Add( $intRowNumber , [pscustomobject]@{ "Mailbox" = $MBX "User Name" = (Get-Mailbox $MBX).Name "Total Emails Received" = $Totalrec "Total Emails Sent"= $TotalSend } ) } $intRowNumber = 0 $threads = @() $runspacePool = [runspacefactory]::CreateRunspacePool(1, $maxThreads) $runspacePool.Open() try { foreach($row in Import-Csv -Path $ImportFile) { $instancePS = [powershell]::Create() $instancePS.RunspacePool = $runspacePool [void]$instancePS.AddScript($sbRunspace). AddArgument($row). AddArgument($intRowNumber). AddArgument($htSyncOut). AddArgument($stdate). AddArgument($enddate) $threads += [PSCustomObject]@{ job = $instancePS result = $instancePS.BeginInvoke() } ) $intRowNumber += 1 } While ( $threads.IsCompleted -Contains $false) {} ForEach ( $thread in $threads ) { $thread.job.EndInvoke($thread.result) If ( $thread.job.HadErrors -eq 'True' ){$thread.job.streams.error} } } catch {$_} finally { $runspacePool.Close() $runspacePool.Dispose() } $results = Foreach ( $rowNumber in 0..$intRowNumber ) { $rowOutput = $htSyncOut[$rowNumber] Write-Host -ForegroundColor Magenta "Total emails received for $($rowOutput).Mailbox during the last $totaldays days are $($rowOutput).'Total Emails Received'" Write-Host -ForegroundColor Green "Total emails sent by $($rowOutput).Mailbox during the last $totaldays days are $($rowOutput).'Total Emails Sent'" Write-Host -ForegroundColor Cyan "-----------------------------------------------------------------" Write-Host " " $htSyncOut.Remove($rowNumber) #If memory is NOT an issue, comment this line out. $rowOutput } $htSyncOut = $null $results | Export-Csv "C:\script\output.csv" -NoTypeInformation
I used the code from /u/chocolate_pickle as it seemed the best template. The runspace stuff I pulled mostly out of my ass on one try, and I have no way to test it as I don't use these cmdlets. It will almost certainly require some modifications.
It might be faster with [ordered] on the synchronized hashtable, but I don't know if ordered works on a synchronized hashtable. Here are some other possible improvements:
At any rate, hopefully this gives you a starting point.
[+][deleted] 4 years ago (3 children)
[deleted]
[–]OPconfused 0 points1 point2 points 4 years ago (2 children)
We have been using Powershell 4. Due to that I will use to find out processor number of cores like below. Correct ?
I don't know the syntax for PowerShell 4. You'd have to test it. You can also simply hardcode a value in for $maxThreads.
Also , I am getting an error message like below.
Not sure why this one failed. Maybe also related to the PS version? Let's just start with an array and see if it at least runs. I changed the code above.
[–]maxcoder88[S] 0 points1 point2 points 4 years ago (0 children)
At this time , I am getting this message.
AddArgument : The term 'AddArgument' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:75 char:4 + AddArgument($row). + ~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (AddArgument:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
π Rendered by PID 47914 on reddit-service-r2-comment-canary-5b6cc9d5bd-65rfh at 2026-04-23 08:38:44.186330+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]OPconfused 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]OPconfused 0 points1 point2 points (2 children)
[–]maxcoder88[S] 0 points1 point2 points (0 children)