Skip to content

Commit 819a7b9

Browse files
Create New-DummyFile.ps1
1 parent 77b2bbe commit 819a7b9

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

New-DummyFile.ps1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<#
2+
.SYNOPSIS
3+
Creates a dummy empty file of a specified size
4+
5+
.DESCRIPTION
6+
This will create one or a number of empty files of a specified size
7+
Useful for creating demos or dummy files to delete as an emergency when
8+
drives are full
9+
10+
.PARAMETER FilePath
11+
The Directory for the files
12+
13+
.PARAMETER Name
14+
The Name of the file, will have underscore integer added if more than one file requested - If you include an extension it will retained otherwise it will have a .dmp extension
15+
16+
.PARAMETER Size
17+
The Size of the File(s) to be created in Mb unless the Gb switch is used
18+
19+
.PARAMETER Number
20+
The number of files to create defaults to 1
21+
22+
.PARAMETER Gb
23+
Switch to change Size to Gb instead of Mb
24+
25+
.PARAMETER Force
26+
OverWrite Existing Files
27+
28+
.EXAMPLE
29+
New-DummyFile -FilePath C:\temp -Name Jeremy -Size 10
30+
31+
Will create 1 file of size 10Mb called jeremy.dmp at C:\temp
32+
33+
.EXAMPLE
34+
New-DummyFile -FilePath C:\temp -Name Jeremy.txt -Size 10 -Number 10
35+
36+
Will create 10 files of size 10Mb called jeremy_N.txt where N is 1-10 at C:\temp
37+
38+
.EXAMPLE
39+
New-DummyFile -FilePath C:\temp -Name Jeremy -Size 10 -Gb
40+
41+
Will create 1 file of size 10Gb called jeremy.dmp at C:\temp
42+
43+
.EXAMPLE
44+
New-DummyFile -FilePath C:\temp -Name Jeremy.txt -Size 10 -Number 10 -Gb
45+
46+
Will create 10 files of size 10Gb called jeremy_N.txt where N is 1-10 at C:\temp
47+
48+
.EXAMPLE
49+
New-DummyFile -FilePath C:\temp -Name Jeremy -Size 10 -Force
50+
51+
Will create 1 file of size 10Mb called jeremy.dmp at C:\temp and overwrite the exisitng file if it exists
52+
53+
.NOTES
54+
SQLDBAWithABeard @SqlDbaWithABeard
55+
#>
56+
57+
function New-DummyFile {
58+
[CmdletBinding(SupportsShouldProcess)]
59+
Param (
60+
[Parameter(Mandatory = $true)]
61+
[string]$FilePath,
62+
[string]$Name = 'DummyFile',
63+
[Parameter(Mandatory = $true)]
64+
[int]$Size,
65+
[int]$Number = 1,
66+
[Switch]$Gb ,
67+
[switch]$Force
68+
)
69+
Begin {
70+
if ($Gb) {
71+
$SizeMsg = "$Size Gb"
72+
}
73+
else {
74+
$SizeMsg = "$Size Mb"
75+
}
76+
Write-Verbose "Creating $Number files of $SizeMsg named $Name at $FilePath "
77+
$fileSize = $Size * 1024 * 1024
78+
if ($Gb) {
79+
$fileSize = $fileSize * 1024
80+
}
81+
}
82+
Process {
83+
for ($i = 1; $i -le $Number; $i++) {
84+
$BaseName, $extension = $Name.Split('.')
85+
if ($null -eq $extension) { $extension = 'dmp' }
86+
$FileName = "$FilePath\$($BaseName)_$i.$Extension"
87+
if (-not $Force) {
88+
if (Test-Path $FileName) {
89+
Write-Warning "Nope I am not creating the file as -Force was not specified and $FileName already exists"
90+
Continue
91+
}
92+
}
93+
if ($PSCmdlet.ShouldProcess("$FilePath", "Creating $FileName ($i/$Number) $FilePath\$FileName")) {
94+
$file = [System.IO.File]::Create($FileName)
95+
$file.SetLength($fileSize)
96+
$file.Close()
97+
}
98+
}
99+
}
100+
End {
101+
Write-Verbose "Done!"
102+
}
103+
}

0 commit comments

Comments
 (0)