This PowerShell script benchmarks the read speed of the C: drive by creating a 1 GB file, reading it multiple times, and calculating the average speed. The results, including date, time, hostname, and average speed, are saved to a CSV file.
# Define the file path and size $filePath = "C:\temp\testfile.tmp" $fileSizeGB = 1 $fileSizeBytes = $fileSizeGB * 1024 * 1024 * 1024 # Create the file with random content $buffer = New-Object byte[] $fileSizeBytes $random = [System.Random]::new() $random.NextBytes($buffer) [System.IO.File]::WriteAllBytes($filePath, $buffer) # Number of times to read the file for averaging $n = 5 $speeds = @() for ($i = 1; $i -le $n; $i++) { # Measure the time to read the file $sw = [System.Diagnostics.Stopwatch]::StartNew() $content = [System.IO.File]::ReadAllBytes($filePath) $sw.Stop() $timeTaken = $sw.Elapsed.TotalSeconds # Calculate speed in MB/s $speedMBps = ($fileSizeBytes / (1024 * 1024)) / $timeTaken $speeds += $speedMBps Write-Host "Read $i $speedMBps MB/s" } # Calculate and display the average speed $averageSpeed = ($speeds | Measure-Object -Average).Average Write-Host "Average read speed: $averageSpeed MB/s" # Clean up by deleting the test file Remove-Item $filePath # Gather additional information $dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $hostname = $env:COMPUTERNAME # Create an object with the data to be saved $result = [PSCustomObject]@{ DateTime = $dateTime Hostname = $hostname AvgSpeedMBps = $averageSpeed } # Define the CSV file path $csvFilePath = "C:\temp\disk_benchmark_results.csv" # Check if the CSV file exists if (Test-Path $csvFilePath) { # Append the data to the existing CSV file $result | Export-Csv -Path $csvFilePath -Append -NoTypeInformation } else { # Create a new CSV file and save the data $result | Export-Csv -Path $csvFilePath -NoTypeInformation }
This will create or append to a CSV file at `C:\temp\disk_benchmark_results.csv` with the date, time, hostname, and average read speed.