-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Format-IndentTabsAsSpaces.ps1
56 lines (45 loc) · 1.23 KB
/
Format-IndentTabsAsSpaces.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Converts tabs to indentation spaces.
.DESCRIPTION
Author: @[email protected] (https://techhub.social/@JamesDBartlett3)
.PARAMETER InputFile
The file to format.
.PARAMETER Indentation
The number of spaces to replace each tab with. Default is 2.
.EXAMPLE
Format-IndentTabsAsSpaces.ps1 -InputFile ".\MyScript.ps1"
.EXAMPLE
# This example will convert all PowerShell scripts in the current directory from tabs to 2 spaces.
Format-IndentTabsAsSpaces.ps1 -InputFile ".\*.ps1"
.EXAMPLE
# This example will convert all PowerShell scripts in the parent directory from tabs to 4 spaces.
Get-ChildItem -Path "..\*.ps1" | Format-IndentTabsAsSpaces.ps1 -Indentation 4
#>
# Parameters
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[ValidateNotNullOrEmpty()]
[string[]]$InputFile,
[Parameter(
Mandatory = $false
)]
[ValidateNotNullOrEmpty()]
[int]$Indentation = 2
)
Begin {
# Set the regex pattern
$pattern = "`t"
$replaceWith = " " * $Indentation
}
# Process
Process {
ForEach ($File in $InputFile) {
(Get-Content $File) | ForEach-Object {
$_ -replace $pattern, $replaceWith
} | Set-Content $File
}
}