forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-BOM-from-files.ps1
102 lines (87 loc) · 2.74 KB
/
remove-BOM-from-files.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
# cleans BOM from start of file in utf encoded files.
# BOM affects use of iwr %script% | iex
# this will convert files with BOM to utf-8 with no BOM
# https://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom
example from iwr script download output
PS C:\Users\cloudadmin> $t = iwr "http://aka.ms/directory-treesize.ps1"
PS C:\Users\cloudadmin> $t | iex
iex : At line:58 char:7
+ do not display output
PS C:\Users\cloudadmin> $t | fl *
Content : {239, 187, 191, 60...}
StatusCode : 200
StatusDescription : OK
RawContentStream : Microsoft.PowerShell.Commands.WebResponseContentMemoryStream
RawContentLength : 18585
RawContent : HTTP/1.1 200 OK
...
X-Powered-By: ASP.NET
issue------>>>> ???<#
.SYNOPSIS
powershell script to to enumerate directory summarizing in tree view directories over a given
size
#>
param(
$path = (get-location).path,
$extensionFilter = "*.ps1",
[switch]$listOnly,
[switch]$saveAsAscii,
[switch]$force
)
$Utf8NoBom = New-Object Text.UTF8Encoding($False)
function main()
{
foreach($file in get-childitem -Path $path -recurse -filter $extensionFilter)
{
$hasBom = has-bom -file $file
if($hasBom -or $saveAsAscii -or $force)
{
if($hasBom)
{
write-host "file has bom: $($file.fullname)" -ForegroundColor Yellow
}
if(!$listOnly)
{
write-warning "re-writing file without bom: $($file.fullname)"
$content = Get-Content $file.fullname -Raw
if($saveAsAscii)
{
out-file -InputObject $content -Encoding ascii -FilePath ($file.fullname)
}
else
{
[System.IO.File]::WriteAllLines($file.fullname, $content, $Utf8NoBom)
}
}
}
else
{
write-host "file does *not* have bom: $($file.fullname)" -ForegroundColor Green
}
}
}
function has-bom($file)
{
[Byte[]]$bom = Get-Content -Encoding Byte -ReadCount 4 -TotalCount 4 -Path $file.fullname
foreach ($encoding in [text.encoding]::GetEncodings().GetEncoding())
{
$preamble = $encoding.GetPreamble()
if ($preamble)
{
foreach ($i in 0..$preamble.Length)
{
if ($preamble[$i] -ne $bom[$i])
{
continue
}
elseif ($i -eq $preable.Length)
{
return $true
}
}
}
}
return $false
}
main