This simple PowerShell script "fills in" the bad sectors of a USB flash drive. But I need to fill the drive with files containing 0x00 (all bits zero) and 0x255 (all bits 1). How can I use hex in PowerShell?
function filler {
Param( [byte]$hex )
$filearray = @()
$count = 1
$freespace = Get-PSDrive H
$maxfiles = [int]($freespace.Free / 1048576)
do {
$randomnum = Get-Random -Minimum 100000000 -Maximum 999999999
"$hex" * 1048576 | Out-File $randomnum
$filecontent = Get-Content $randomnum -Raw
if ($filecontent -notcontains ('$hex')) {
# do nothing because the content is incorrect
} else {
$filearray += $randomnum
}
$count++
} while ($count -le $maxfiles)
foreach ($filename in $filearray) {
Remove-Item $filename
}
}
filler -hex 0x00
filler -hex 0xFF
[byte]$hex = 0xff; "$hex" * 5doesn't do what you seem to expect. It creates the string "255255255255255" (repeat the string representation of the integer value 0xFF 5 times).