1
0
mirror of https://github.com/jaapbrasser/SharedScripts.git synced 2025-12-24 21:51:38 +02:00
Files
SharedScripts/New-ZeroFile/New-ZeroFile.ps1

90 lines
2.7 KiB
PowerShell
Raw Permalink Normal View History

2017-04-25 15:57:16 +02:00
function New-ZeroFile {
<#
.SYNOPSIS
Creates an empty, zeroed out file
2017-04-25 15:57:16 +02:00
.DESCRIPTION
This function serves as a wrapper for fsutil, and can be used to create an empty file to reserve disk space
2017-04-25 15:57:16 +02:00
.PARAMETER Path
The path and file name of the zero file that will be created
.PARAMETER FileSize
2017-10-06 11:47:22 +09:00
Specifies the size, in bytes, of the file, 1KB / 1MB / 1GB / 1TB notatation can be used
2017-04-25 15:57:16 +02:00
.PARAMETER Force
Overwrites an existing file if the specified path already exists
2017-04-25 15:57:16 +02:00
2017-09-30 11:25:13 +09:00
.PARAMETER OutputObject
Returns the created file object so that this function can be used in a pipeline
.NOTES
2017-04-25 15:57:16 +02:00
Name: New-ZeroFile
Author: Jaap Brasser
DateCreated: 2017-01-26
2017-10-01 12:28:16 +09:00
DateUpdated: 2017-10-01
Version: 1.1.1
2017-04-25 15:57:16 +02:00
Blog: http://www.jaapbrasser.com
.LINK
http://www.jaapbrasser.com
2017-09-30 11:25:13 +09:00
.EXAMPLE
New-ZeroFile -Path c:\temp\j2.file -FileSize 500mb
Description
-----------
Creates a 500MB file in C:\Temp named jb.file
.EXAMPLE
New-ZeroFile -Path c:\temp\jb2.file,c:\temp\jb3.file -FileSize 1gb -Verbose
2017-04-25 15:57:16 +02:00
Description
-----------
2017-09-30 11:25:13 +09:00
Creates two 1gb files in the C:\Temp folder: jb2.file and jb3.file and displays verbose information
.EXAMPLE
'c:\temp\jb4.file','c:\temp\jb5.file' | New-ZeroFile -FileSize 10mb -OutputObject -Force
Description
-----------
Creates two 10mb files in the C:\Temp folder: jb3.file and jb4.file. Overwriting existing files if they are there and displaying the created fileinfo objects
#>
2017-04-25 15:57:16 +02:00
2017-09-30 11:25:13 +09:00
[CmdletBinding()]
2017-04-25 15:57:16 +02:00
param(
2017-09-30 11:25:13 +09:00
[Parameter(
2017-10-01 12:28:16 +09:00
Mandatory = $true,
ValueFromPipeline = $true
2017-09-30 11:25:13 +09:00
)]
[string[]] $Path,
[Parameter(
2017-10-01 12:28:16 +09:00
Mandatory = $true
2017-09-30 11:25:13 +09:00
)]
[alias('Size','FS')]
[long] $FileSize,
[switch] $Force,
[switch] $OutputObject
2017-04-25 15:57:16 +02:00
)
2017-09-30 11:25:13 +09:00
process {
foreach ($CurrentPath in $Path) {
if ((Test-Path -LiteralPath $CurrentPath) -and $Force) {
Write-Verbose "Overwriting existing file '$CurrentPath'"
Remove-Item -LiteralPath $CurrentPath -Force
$null = fsutil.exe file createnew ""$CurrentPath"" $FileSize
} elseif (Test-Path -LiteralPath $CurrentPath) {
2017-10-01 12:28:16 +09:00
Write-Warning "The file '$CurrentPath' already exists, no action taken" -WarningVariable Warning
2017-09-30 11:25:13 +09:00
} else {
Write-Verbose "Creating new file '$CurrentPath'"
$null = fsutil.exe file createnew ""$CurrentPath"" $FileSize
2017-10-01 12:28:16 +09:00
}
2017-04-25 15:57:16 +02:00
2017-10-01 12:28:16 +09:00
# Output object if file has been created and output object switch is set
if ($OutputObject -and $Warning -notmatch [regex]::Escape($CurrentPath)) {
Get-Item -LiteralPath $CurrentPath
2017-09-30 11:25:13 +09:00
}
}
}
2017-04-25 15:57:16 +02:00
}