-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathOther.ps1
1271 lines (1108 loc) · 45.8 KB
/
Other.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Re-register all UWP apps
$Bundles = (Get-AppXPackage -PackageTypeFilter Framework -AllUsers).PackageFullName
Get-ChildItem -Path "HKLM:\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\PackageRepository\Packages" | ForEach-Object -Process {
Get-ItemProperty -Path $_.PSPath
} | Where-Object -FilterScript {$_.Path -match "Program Files"} | Where-Object -FilterScript {$_.PSChildName -notin $Bundles} | Where-Object -FilterScript {$_.Path -match "x64"} | ForEach-Object -Process {
"$($_.Path)\AppxManifest.xml"
} | Add-AppxPackage -Register -ForceApplicationShutdown -ForceUpdateFromAnyVersion -DisableDevelopmentMode -Verbose
#
$PackagesIds = [Windows.Management.Deployment.PackageManager, Windows.Web, ContentType = WindowsRuntime]::new().FindPackages() | Select-Object -Property DisplayName -ExpandProperty Id | Select-Object -Property Name, DisplayName
foreach ($AppxPackage in $AppxPackages)
{
"$($AppxPackage.InstallLocation)\AppxManifest.xml" | Add-AppxPackage -Register -ForceApplicationShutdown -ForceUpdateFromAnyVersion -DisableDevelopmentMode -Verbose
}
# Check for UWP apps updates
Get-CimInstance -Namespace root/CIMV2/mdm/dmmap -ClassName MDM_EnterpriseModernAppManagement_AppManagement01 | Invoke-CimMethod -MethodName UpdateScanMethod
# Find all non-USB & non-boot drives except nulled letter drives (external USB drives are excluded)
(Get-Disk | Where-Object -FilterScript {($_.BusType -ne "USB") -and (-not $_.IsBoot)} | Get-Partition | Get-Volume | Where-Object -FilterScript {$_.DriveLetter}).DriveLetter
# Add domains to hosts
$hosts = "$env:SystemRoot\System32\drivers\etc\hosts"
$Domains = @("site.com", "site2.com")
foreach ($Domain in $Domains)
{
if (-not (Get-Content -Path $hosts -Force | Select-String -SimpleMatch "0.0.0.0 `t $Domain"))
{
Add-Content -Path $hosts -Value "0.0.0.0 `t $Domain" -Force
}
}
# Split the name from the path
Split-Path -Path file.ext -Leaf
# Split the path from the name
Split-Path -Path file.ext -Parent
# Split the last folder name from the path
Get-Item -Path file.ext | Split-Path -Parent | Split-Path -Parent | Split-Path -Leaf
# Split the drive letter
Split-Path -Path "D:\file.mp3" -Qualifier
# Events categories
enum Level
{
LogAlways = 0
Critical = 1
Error = 2
Warning = 3
Informational = 4
Verbose = 5
}
# [Level]::LogAlways.value__
# [Level]0
# WinEvent
# https://schneegans.de/windows/process-audit/
# https://devblogs.microsoft.com/commandline/how-to-determine-what-just-ran-on-windows-console/
$Level = @{
Name = "Level"
Expression = {[Level]$_.Level}
}
Get-WinEvent -LogName System | Select-Object Id, $Level, ProviderName, ThreadId, LevelDisplayName, TaskDisplayName
Get-WinEvent -LogName System | Where-Object -FilterScript {$_.LevelDisplayName -match "Критическая" -or $_.LevelDisplayName -match "Ошибка"}
#
$WindowsPowerShell = @{
LogName = "Windows PowerShell"
ProviderName = "PowerShell"
Id = "800"
}
Get-WinEvent -FilterHashtable $WindowsPowerShell | Where-Object -FilterScript {$_.Level -eq "3" -or $_.Level -eq "4"}
#
Get-WinEvent -LogName "Windows PowerShell" | Where-Object -FilterScript {$_.Message -match "HostApplication=(?<a>.*)"} | Format-List -Property *
#
$Security = @{
LogName = "Security"
Id = 4688
}
$NewProcessName = @{
Name = "NewProcessName"
Expression = {$_.Properties[5].Value}
}
$CommandLine = @{
Name = "CommandLine"
Expression = {$_.Properties[8].Value}
}
Get-WinEvent -FilterHashtable $Security | Select-Object TimeCreated, $NewProcessName, $CommandLine | Format-Table -AutoSize -Wrap
#
function Get-ProcessAuditEvents ([long]$MaxEvents)
{
function Prettify([string]$Message)
{
$Message = [regex]::Replace($Message, '\s+Token Elevation Type indicates.+$', '', [System.Text.RegularExpressions.RegexOptions]::Singleline)
$Message = [regex]::Replace($Message, '(Token Elevation Type:\s+%%1936)', '$1 (Full token)')
$Message = [regex]::Replace($Message, '(Token Elevation Type:\s+%%1937)', '$1 (Elevated token)')
$Message = [regex]::Replace($Message, '(Token Elevation Type:\s+%%1938)', '$1 (Limited token)')
return $Message
}
$Security = @{
LogName = "Security"
Id = 4688
}
Get-WinEvent -MaxEvents $MaxEvents -FilterHashtable $Security | Sort-Object -Property TimeCreated | ForEach-Object {
[pscustomobject] @{
TimeCreated = $_.TimeCreated
Message = $_.Message
}
}
}
Get-ProcessAuditEvents -MaxEvents 10 | Format-List
#
$ParentProcess = @{
Label = "ParentProcess"
Expression = {$_.Properties[13].Value}
}
Get-WinEvent -LogName Security | Where-Object -FilterScript {$_.Id -eq "4688"} | Where-Object -FilterScript {$_.Properties[5].Value -match 'conhost'} | Select-Object TimeCreated, $ParentProcess | Select-Object -First 10
# Enable Event Log
$Log = Get-LogProperties -Name Application
$Log.Enabled = $true
Set-LogProperties -LogDetails $logsource
# & wevtutil.exe set-log Application /e
# Get string hash
function Get-StringHash
{
[CmdletBinding()]
param
(
[string]
$String,
[ValidateSet("MACTripleDES", "MD5", "RIPEMD160", "SHA1", "SHA256", "SHA384", "SHA512")]
[string]
$Hash
)
$HashResalt = [System.Security.Cryptography.HashAlgorithm]::Create($Hash).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))
[System.BitConverter]::ToString($HashResalt).Replace('-', '')
}
Get-StringHash -String 2 -HashName SHA1
# Encode using Base64 and vice versa
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("SecretMessage"))
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("U2VjcmV0TWVzc2FnZQ=="))
# Expand the window with "Task manager" title but others to minimize
$Win32ShowWindowAsync = @{
Namespace = "WinAPI"
Name = "Win32ShowWindowAsync"
Language = "CSharp"
MemberDefinition = @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
}
if (-not ("WinAPI.Win32ShowWindowAsync" -as [type]))
{
Add-Type @Win32ShowWindowAsync
}
$title = "Task Manager"
Get-Process | Where-Object -FilterScript {$_.MainWindowHandle -ne 0} | ForEach-Object -Process {
if ($_.MainWindowTitle -eq $title)
{
[WinAPI.Win32ShowWindowAsync]::ShowWindowAsync($_.MainWindowHandle, 3) | Out-Null
}
else
{
[WinAPI.Win32ShowWindowAsync]::ShowWindowAsync($_.MainWindowHandle, 6) | Out-Null
}
}
# Set a window state
# https://docs.microsoft.com/ru-ru/windows/win32/api/winuser/nf-winuser-showwindow
function WindowState
{
[CmdletBinding()]
param
(
[Parameter(
ValueFromPipeline = $true,
Mandatory = $true,
Position = 0
)]
[ValidateScript({$_ -ne 0})]
[System.IntPtr]
$MainWindowHandle,
[ValidateSet(
"FORCEMINIMIZE", "HIDE", "MAXIMIZE", "MINIMIZE", "RESTORE",
"SHOW", "SHOWDEFAULT", "SHOWMAXIMIZED", "SHOWMINIMIZED",
"SHOWMINNOACTIVE", "SHOWNA", "SHOWNOACTIVATE", "SHOWNORMAL"
)]
[string]
$State = "SHOW"
)
$WindowStates = @{
"HIDE" = 0 # Скрыть окно и активизировать другое окно
"SHOWNORMAL" = 1 # Активизировать и отобразить окно, если окно свернуто или развернуто
"SHOWMINIMIZED" = 2 # Отобразить окно в свернутом виде
"MAXIMIZE" = 3 # Maximizes the specified window
"SHOWMAXIMIZED" = 3 # Activates the window and displays it as a maximized window
"SHOWNOACTIVATE" = 4 # Отобразить окно в соответствии с последними значениями позиции и размера. Активное окно остается активным
"SHOW" = 5 # Активизировать окно
"MINIMIZE" = 6 # Свернуть окно и активизировать следующее окно в Z-порядке (следующее под свернутым окном)
"SHOWMINNOACTIVE" = 7
"SHOWNA" = 8 # Отобразить окно в текущем состоянии. Активное окно остается активным
"RESTORE" = 9 # Активизировать и отобразить окно. Если окно свернуто или развернуто, Windows восстанавливает его исходный размер и положение
"SHOWDEFAULT" = 10 # (1+9) Активизировать и отобразить окно на переднем плане, если было свернуто или скрыто
"FORCEMINIMIZE" = 11 # Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread
}
$Win32ShowWindowAsync = @{
Namespace = "Win32Functions"
Name = "Win32ShowWindowAsync"
Language = "CSharp"
MemberDefinition = @"
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
"@
}
if (-not ("Win32Functions.Win32ShowWindowAsync" -as [type]))
{
Add-Type @Win32ShowWindowAsync
}
[Win32Functions.Win32ShowWindowAsync]::ShowWindowAsync($MainWindowHandle , $WindowStates[$State])
}
$MainWindowHandle = (Get-Process -Name notepad | Where-Object -FilterScript {$_.MainWindowHandle -ne 0}).MainWindowHandle
$MainWindowHandle | WindowState -State HIDE
# Become a file owner
takeown /F D:\file.exe
icacls D:\file.exe /grant:r %username%:F
# Become a folder owner
takeown /F C:\HV\10 /R
icacls C:\HV\10 /grant:r %username%:F /T
# Search for a file on all local drives and its' full path
$file = "file.ext"
(Get-ChildItem -Path ([System.IO.DriveInfo]::GetDrives() | Where-Object {$_.DriveType -ne "Network"}).Name -Recurse -ErrorAction SilentlyContinue | Where-Object -FilterScript {$_.Name -like "$file"}).FullName
# Remove the first $c letters in the file names in a folder
$Path = "D:\folder"
$Extension = "flac"
$Characters = 4
(Get-ChildItem -LiteralPath $Path -Filter *.$Extension) | Rename-Item -NewName {$_.Name.Substring($Characters)}
# Remove the last $c letters in the file names in a folder
$Path = "D:\folder"
$Extension = "flac"
$Characters = 4
Get-ChildItem -LiteralPath $Path -Filter *.$Extension | Rename-Item -NewName {$_.Name.Substring(0,$_.BaseName.Length-$Characters) + $_.Extension}
# Find files in the name of which every word isn't capitalized
$Path = "D:\folder"
(Get-ChildItem -LiteralPath $Path -File -Recurse | Where-Object -FilterScript {($_.BaseName -replace "'|``") -cmatch "\b\p{Ll}\w*"}).FullName
# Capitize the first letter for every word in the files names in a folder
$Path = "D:\folder"
$Extension = "flac"
Get-ChildItem -Path $Path -Filter *.$Extension | Rename-Item -NewName {(Get-Culture).TextInfo.ToTitleCase($_.BaseName) + $_.Extension}
# Capitalize the first letters
$String = "аа аа аа"
(Get-Culture).TextInfo.ToTitleCase($String.ToLower())
# Count chars in a string
("string" | Measure-Object -Character).Characters
# Replace a word in a file name in a folder
Get-ChildItem -Path "D:\folder" | Rename-Item -NewName {$_.Name.Replace("abc","cba")}
# Replace an extension name in a folder
$Path = "D:\folder"
Get-ChildItem -Path $Path | Rename-Item -NewName {$_.FullName.Replace(".txt1",".txt")}
# Add REG_NONE
New-ItemProperty -Path HKCU:\Software -Name Name -PropertyType None -Value ([byte[]]@()) -Force
# Binary
"50,33,01".Split(",") | ForEach-Object -Process {"0x$_"}
#
$int = 0x6054b50
$bytes = [System.BitConverter]::GetBytes($int)
$int = [System.BitConverter]::ToInt32($bytes, 0)
'0x{0:x}' -f $int
# Find all uninstalled updates
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$Updates = @($UpdateSearcher.Search("IsHidden=0 and IsInstalled=0").Updates)
$Updates | Select-Object -ExpandProperty Title
# Trigger Windows Update for detecting new updates
# https://michlstechblog.info/blog/windows-10-trigger-detecting-updates-from-command-line
# https://omgdebugging.com/2017/10/09/command-line-equivalent-of-wuauclt-in-windows-10-windows-server-2016/
Start-Process -FilePath "$env:SystemRoot\System32\UsoClient.exe" -ArgumentList StartInteractiveScan
# Exclude KB update from installing
(New-Object -ComObject Microsoft.Update.Session).CreateUpdateSearcher().Search("IsHidden = 0").Updates | Where-Object -FilterScript {$_.KBArticleIDs -eq "5005463"} | ForEach-Object -Process {$_.IsHidden = $true}
# Check for a Windows Update pending reboot
$Parameters = @{
Namespace = "root\CIMv2"
ClassName = "StdRegProv"
MethodName = "EnumKey"
}
$Parameters.Arguments = @{
hDefKey = [UInt32]2147483650
sSubKeyName = "SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing"
}
(Invoke-CimMethod @Parameters).sNames -contains "RebootPending"
$Parameters.Arguments = @{
hDefKey = [UInt32]2147483650
sSubKeyName = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
}
(Invoke-CimMethod @Parameters).sNames -contains "RebootRequired"
# Get KB update status
$Setup = @{
LogName = "Setup"
ProviderName = "Microsoft-Windows-Servicing"
}
foreach ($KBid in @("KB5026446"))
{
Get-WinEvent -FilterHashTable $Setup | Where-Object -FilterScript {$_.Message -like "*$KBid*"} | Format-Table -Wrap
}
#
Get-WindowsPackage -Online -PackageName Package_for_RollupFix* | Sort-Object -Descending | Format-Table PackageName, InstallTime, PackageState, SupportInformation
# Get Windows updates installed
Get-CimInstance -ClassName Win32_ReliabilityRecords
Get-CimInstance -ClassName Win32_QuickFixEngineering
Get-HotFix
Get-Package -ProviderName msu
#
$Session = New-Object -ComObject "Microsoft.Update.Session"
# [Activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session"))
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
if ($HistoryCount -gt 0)
{
$Searcher.QueryHistory(0,$HistoryCount)
}
# Closed the specific File Explorer window
$FolderName = "D:\folder"
(New-Object -ComObject "Shell.Application").Windows() | Where-Object {$_.Document.Folder.Self.Path -eq $FolderName} | ForEach-Object -Process {$_.Quit()}
# StartsWith/EndsWith
$String = "1234"
$String.StartsWith("1")
$String.EndsWith("4")
# Context menu verbs
$Target = Get-Item -Path "D:\folder\file.lnk"
$Shell = New-Object -ComObject Shell.Application
$Folder = $Shell.NameSpace($Target.DirectoryName)
$file = $Folder.ParseName($Target.Name)
$Verb = $File.Verbs() | Where-Object -FilterScript {$_.Name -like "Закрепить на начальном &экране"}
$Verb.DoIt()
# Convert hash table into objects
$hash = @{
Name = "Tobias"
Age = 66
Status = "Online"
}
New-Object -TypeName PSObject -Property $hash
# Remove unremovable registry key
$parent = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1', $true)
$parent.DeleteSubKey('UserChoice', $true)
$parent.Close()
# Drives properties
Get-PhysicalDisk | Get-StorageReliabilityCounter | Select-Object -Property *
# Get NVidia videocard temperature
nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader
# Get disks temperature
Get-PhysicalDisk | Get-Disk | ForEach-Object -Process {
[PSCustomObject]@{
Disk = $_.FriendlyName
Temperature = (Get-PhysicalDisk -DeviceNumber $_.DiskNumber | Get-StorageReliabilityCounter).Temperature
}
}
# Show all autostarts. Even drivers
Get-EventLog -LogName System -InstanceId 1073748869 | ForEach-Object {
[PSCustomObject]@{
Date = $_.TimeGenerated
Name = $_.ReplacementStrings[0]
Path = $_.ReplacementStrings[1]
StartMode = $_.ReplacementStrings[3]
User = $_.ReplacementStrings[4]
}
}
# break, continue, return, exit
function Test-Function
{
$fishtank = 1..10
foreach ($fish in $fishtank)
{
if ($fish -eq 7)
{
# break # abort loop
# continue # skip just this iteration, but continue loop
# return # abort code, and continue in caller scope
# exit # abort code at caller scope
}
"fishing fish #$fish"
}
"Done"
}
Test-Function
"Script done"
# Find all notepad.exe processes, convert into an array and kill all
@(Get-Process -Name Notepad).ForEach({Stop-Process -InputObject $_})
# Compare hashes from .cat files
$HT = @{
CatalogFilePath = "D:\file.cat"
Path = "D:\folder"
Detailed = $true
FilesToSkip = "file.xml"
}
Test-FileCatalog @HT
# Reset local user password via WinPE
# In the WinPE
MOVE C:\Windows\system32\utilman.exe C:\Windows\system32\utilman.exe.bak
RENAME C:\Windows\system32\utilman.exe utilman.exe.bak
COPY C:\Windows\system32\cmd.exe C:\Windows\system32\utilman.exe
wpeutil reboot
#
$user = (Get-LocalUser | Where-Object -FilterScript {$_.Enabled}).Name
$user
$Password = Read-Host -Prompt "Enter the new password" -AsSecureString
Get-LocalUser -Name $user | Set-LocalUser -Password $Password
# WinPE
DEL C:\Windows\system32\utilman.exe /F
RENAME C:\Windows\system32\utilman.exe.bak utilman.exe
# Restoring components
Repair-WindowsImage -Online -RestoreHealth
DISM /Online /Cleanup-Image /RestoreHealth
# Restoring components locally
DISM /Get-WimInfo /WimFile:E:\sources\install.wim
Get-WindowsImage -ImagePath "E:\sources\install.wim"
DISM /Online /Cleanup-Image /RestoreHealth /Source:E:\sources\install.wim:1 /LimitAccess
Repair-WindowsImage -Online -RestoreHealth -Source E:\sources\install.wim:1 -LimitAccess
# Restoring system files
sfc /scannow
# Restoring components in the Windows PE
DISM /Get-WimInfo /WimFile:E:\sources\install.wim
DISM /Image:C:\ /Cleanup-Image /RestoreHealth /Source:E:\sources\install.wim:3 /ScratchDir:C:\mnt
# Restoring system files in the Windows PE
sfc /scannow /offbootdir=C:\ /offwindir=C:\Windows
# WinSxS cleaning up
DISM /Online /Cleanup-Image /StartComponentCleanup /ResetBase
# Check if a file is saved in UTF-8 with BOM encoding
$bytes = Get-Content -Path $file -Encoding Byte -Raw
if (($bytes[0] -ne 239) -and ($bytes[1] -ne 187) -and ($bytes[2] -ne 191))
{
Write-Warning -Message "The script wasn't saved in `"UTF-8 with BOM`" encoding"
}
# Write-Progress
$ExcludedAppxPackages = @(
"NVIDIACorp.NVIDIAControlPanel"
)
$OFS = "|"
$AppxPackages = (Get-AppxPackage -PackageTypeFilter Bundle -AllUsers).Name | Select-String $ExcludedAppxPackages -NotMatch
foreach ($AppxPackage in $AppxPackages)
{
Write-Progress -Activity "Uninstalling UWP apps" -Status "Removing $AppxPackage" -PercentComplete ($AppxPackages.IndexOf($AppxPackage)/$AppxPackages.Count * 100)
Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Where-Object -FilterScript {$_.Name -cmatch $AppxPackage} | Remove-AppxPackage -AllUsers
}
Write-Progress -Activity "Uninstalling UWP apps" -Completed
# Waiting for a process
do
{
$Process = Get-Process -Name notepad
if ($Process)
{
Write-Host "Running: $($Process.Name)"
Start-Sleep -Milliseconds 500
}
}
until (-not ($Process))
#
while ($true)
{
$Process = Get-Process -Name notepad
if ($Process)
{
Write-Host "Running: $($Process.Name)"
Start-Sleep -Milliseconds 500
}
}
#
$Process = Get-Process -Name notepad
while
(
$($Process.Refresh()
$Process.ProcessName)
)
{
Write-Host "Running: $($Process.Name)"
Start-Sleep -Milliseconds 500
}
#
while ($true)
{
if ((Get-Process -Name notepad++ -ErrorAction Ignore) -and (-not (Get-Process -Name notepad -ErrorAction Ignore)))
{
# if notepad++ is running, and notepad is not, then run notepad.exe
Start-Process -FilePath "$env:SystemRoot\system32\notepad.exe"
}
elseif ((-not (Get-Process -Name notepad++ -ErrorAction Ignore)) -and (Get-Process -Name notepad -ErrorAction Ignore))
{
# notepad++ isn't runn, and notepad.exe is, then kill notepad.exe
Stop-Process -Name notepad -Force -ErrorAction Ignore
}
Start-Sleep -Milliseconds 500
}
# for
do
{
$Prompt = Read-Host -Prompt " "
if ([string]::IsNullOrEmpty($Prompt))
{
break
}
else
{
switch ($Prompt)
{
"Y" {}
"N" {}
Default {}
}
}
}
while ($Prompt -ne "N")
# Compare binary values
((Get-ItemPropertyValue -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer -Name link) -join " ") -ne ([byte[]](00, 00, 00, 00) -join " ")
# Get UEFI license key
(Get-CimInstance -ClassName SoftwareLicensingService).OA3xOriginalProductKey
# Activate Windows
slmgr.vbs /skms <servername>
slmgr.vbs /ato
# Get exception name
$Error.Exception.GetType().FullName
(Get-Error).Exception.Type
# Close all windows without killing the File Explorer process
(New-Object -ComObject Shell.Application).Windows() | Where-Object {$null -ne $_.FullName} | Where-Object {$_.FullName.EndsWith("\explorer.exe") } | ForEach-Object -Process {$_.Quit()}
# Show table with files names and lines count in a folder. -Raw reads blank lines
$FullName = @{
Name = "File"
Expression = {$_.FullName}
}
$Lines = @{
Name = "Lines"
Expression = {Get-Content -Path $_.FullName -Raw | Measure-Object -Line | Select-Object -ExpandProperty Lines}
}
Get-ChildItem -Path "D:\Folder" -Depth 0 -File -Filter *.psd1 -Recurse -Force | ForEach-Object -Process {$_ | Select-Object -Property $FullName, $Lines} | Format-Table -AutoSize
# Show table with files names and lines count in a folder except the specific folder
$FullName = @{
Name = "File"
Expression = {$_.FullName}
}
$Lines = @{
Name = "Lines"
Expression = {Get-Content -Path $_.FullName -Raw | Measure-Object -Line | Select-Object -ExpandProperty Lines}
}
Get-ChildItem -Path "D:\Folder" -Recurse -File -Force | Where-Object -FilterScript {$_.PSParentPath -notmatch "sophos"} | ForEach-Object -Process {$_ | Select-Object -Property $FullName, $Lines} | Format-Table -AutoSize
(Get-ChildItem -Path "D:\Folder" -Recurse -File -Force | Where-Object -FilterScript {$_.PSParentPath -notmatch "sophos"} | ForEach-Object -Process {(Get-Content -Path $_.FullName).Count} | Measure-Object -Sum).Sum
# Count the total number of lines in all files in all subfolders
$i = 0
Get-ChildItem -Path "D:\folder" -Depth 0 -Exclude *.dll, *.winmd, *.ps1 -File -Recurse -Force | ForEach-Object -Process {
(Get-Content -Path $_.FullName -Raw | Measure-Object -Line).Lines | ForEach-Object -Process {
$i += $_
}
}
Write-Verbose -Message "Total number of lines: $i" -Verbose
#
$a = 0
Get-ChildItem -Path D:\Sophia-Script-for-Windows\src -File -Recurse | ForEach-Object -Process {
$a += ((Get-Content -Path $_.FullName).Count | Measure-Object -Sum).Sum
}
Write-Verbose -Message "Total number of lines: $i" -Verbose
# Error description
function Convert-Error ([int]$ErrorCode)
{
CertUtil -error $ErrorCode
"`n"
New-Object -TypeName System.ComponentModel.Win32Exception($ErrorCode)
}
Convert-Error -2147287037
# Remove lines starting with "//" and blank spaces
Get-Content -Path $settings | Where-Object -FilterScript {$_ -notmatch "//"} | Where-Object -FilterScript {$_.Trim(" `t")} | Set-Content -Path $settings -Force
# Quote every item
Get-Content -Path D:\file.txt -Force | ForEach-Object -Process {"'$_'"}
# PowerShell 7.x
@("1", "2", "3") | Join-String -Property $_ -DoubleQuote -Separator ', '
# Restore every files from Defender quarantine to their origin location
# https://docs.microsoft.com/en-us/windows/security/threat-protection/microsoft-defender-antivirus/command-line-arguments-microsoft-defender-antivirus
(Get-MpThreat).Resources.Replace('file:_',"") | ForEach-Object -Process {
# Start-Sleep -Seconds 3
Start-Process -FilePath "$env:ProgramFiles\Windows Defender\MpCmdRun.exe" -ArgumentList @("-Restore -FilePath `"$_`"") -Wait
}
# Insert an XML node
[xml]$XML1 = @"
<toast duration="$ToastDuration" scenario="reminder">
<visual>
<binding template="ToastGeneric">
<group>
<subgroup>
<text hint-style="body" hint-wrap="true" >$EventText</text>
</subgroup>
</group>
</binding>
</visual>
</toast>
"@
[xml]$XML2 = @"
<toast>
<actions>
<input id="SnoozeTimer" type="selection" title="Select a Snooze Interval" defaultInput="1">
<selection id="1" content="1 Minute"/>
</input>
<action activationType="system" arguments="snooze" hint-inputId="SnoozeTimer" content="$SnoozeTitle" id="test-snooze"/>
</actions>
</toast>
"@
$XML1.toast.AppendChild($XML1.ImportNode($XML2.toast.actions, $true))
$XML1.Save("C:\1.xml")
# Validate all .psd1 in all folders
$Folder = Get-ChildItem -Path "D:\Desktop\Sophia Script" -Recurse -Include *.psd1
foreach ($Item in $Folder.DirectoryName)
{
Import-LocalizedData -FileName Sophia.psd1 -BaseDirectory $Item -BindingVariable Data
}
# Adding and Removing Items from a PowerShell Array
# https://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html
$Fruits = "Apple", "Pear", "Banana", "Orange"
$Fruits.GetType()
$Fruits.Add("Kiwi")
# $Fruits.Remove("Apple")
$Fruits.IsFixedSize
$Fruits = $Fruits -ne "Apple"
$Fruits
$Fruits = {$Fruits}.Invoke()
$Fruits.GetType()
$Collection.Add("Melon")
$Collection.Remove("Apple")
$Collection
# Arrays
$Fruits = "Apple","Pear","Banana","Orange"
$Fruits.GetType()
$Fruits.Add("Kiwi")
$Fruits.Remove("Apple")
$Fruits.IsFixedSize
[System.Collections.ArrayList]$ArrayList = $Fruits
$ArrayList.GetType()
$ArrayList.Add("Kiwi")
$ArrayList
$ArrayList.Remove("Apple")
$ArrayList
# Convert an array into System.Collections.ObjectModel.Collection`1
$Collection = {$Fruits}.Invoke()
$Collection
$Collection.GetType()
$Collection.Add("Melon")
$Collection
$Collection.Remove("Apple")
$Collection
# Disable NTFS compression in all subfolders
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/compact
$Paths = Get-ChildItem -Path D:\Folder -Recurse -Directory -Force
foreach ($Path in $Paths.FullName)
{
$RecurseArgument = ('/S:{0}' -f $Path)
& compact.exe /U $RecurseArgument
}
# Disable NTFS compression for the parent subfolder
$ParentFolder = Split-Path -Path $Paths.FullName -Parent
& compact.exe /U $ParentFolder
# Isolate IP addresses only
$Array = @('Handshake', 'Success', 'Status', 200, '192.30.253.113', 'OK', 0xF, "2001:4860:4860::8888")
$Array | Where-Object -FilterScript {-not ($_ -as [Double]) -and ($_ -as [IPAddress])}
# Download and install all Store related UWP packages. Even for LTSC
# Reboot required about execution
wsreset -i
# Since Windows 22H2 22557 build
# https://oofhours.com/2022/04/27/language-pack-handling-in-windows-11-continues-to-evolve/
# https://en.wikipedia.org/wiki/IETF_language_tag
# LanguagePackManagement module
Install-Language -Language en-US
Get-InstalledLanguage
Set-SystemPreferredUILanguage
Get-SystemPreferredUILanguage
Uninstall-Language
# Bypass the Internet account creation in Windows 11
# Shift+F10
OOBE\BYPASSNRO
# Windows 11 Insider Preview 25120+
# This method requires to invoke the following commands if user doesn't use password
# Otherwise even if password is blank OS will ask to prolong it and block access
# Set-LocalUser -Name $env:USERNAME -PasswordNeverExpires $true
# net user $env:USERNAME /passwordreq:no
# Add a new user account
net user username /add
# Администраторы
net localgroup Administrators username /add
# "Пользователи удаленного рабочего стола"
net localgroup "Remote Desktop Users" username /add
net user $env:USERNAME /passwordreq:no
# Prevent Windows to restart automatically after a system failure
# The parameter EnableAllvileges allows us to manipulate the properties of this WMI object if the current Powershell host runs as Administrator
Get-CimInstance -ClassName Win32_OSRecoveryConfiguration | Set-CIMInstance -Arguments @{AutoReboot = $false}
# Send firmware to HP MFU to upgrade
copy /b D:\firmware.rfu \\nt_server\MFU
# Create a table with WSL installed distros
[System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode
# https://github.com/microsoft/WSL/blob/master/distributions/DistributionInfo.json
# wsl --list --online relies on Internet connection too, so it's much convenient to parse DistributionInfo.json, rather than parse a cmd output
$Parameters = @{
Uri = "https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json"
UseBasicParsing = $true
Verbose = $true
}
$Distros = (Invoke-RestMethod @Parameters).Distributions | ForEach-Object -Process {
[PSCustomObject]@{
"Distro" = $_.FriendlyName
"Alias" = $_.Name
}
}
($Distros | Where-Object -FilterScript {$_.Distro -eq "Ubuntu"}).Alias
# $Distros | ConvertTo-Json
# $Distros | ForEach-Object -Process {(wsl --list --quiet) -contains $_.Alias}
<#
$Extensions = @{
"Ubuntu" = "Ubuntu"
"Debian GNU/Linux" = "Debian"
"Kali Linux Rolling" = "kali-linux"
"Linux Enterse Server v12" = "SLES-12SUSE"
"Linux Enterse Server v15" = "SLES-15SUSE"
"Ubuntu 18.04 LTS" = "Ubuntu-18.04"
"Ubuntu 20.04 LTS" = "Ubuntu-20.04"
"Ubuntu 22.04 LTS " = "Ubuntu-22.04"
"Linux 8.5" = "OracleLinux_8_5Oracle"
"Linux 7.9" = "OracleLinux_7_9Oracle"
}
$Extensions.Keys | ForEach-Object -Process {(wsl --list --quiet) -contains $_}
#
[System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode
$wsl = wsl --list --online
# Calculate the string number where the "FRIENDLY NAME" header begins to truncate all other unnecessary strings in the beginning
$LineNumber = ($wsl | Select-String -Pattern "FRIENDLY NAME" -CaseSensitive).LineNumber
# Remove first strings in output from the first to the $LineNumber
$Distros = ($wsl).Replace(" ", "").Replace("* ", "")[($LineNumber)..(($wsl).Count)] | ForEach-Object -Process {
[PSCustomObject]@{
"Distro" = ($_ -split " ", 2 | Select-Object -Last 1).Trim()
"Alias" = ($_ -split " ", 2 | Select-Object -First 1).Trim()
}
}
#>
# Save PSCustomObject to a variable
$ActiveDirectoryList = @()
"Testvm1", "Testvm2", "Testvm3" | ForEach-Object -Process {
$Var = [PSCustomObject]@{
VMName = $_
Location = 'EastUS'
}
$ActiveDirectoryList += $Var
}
$ActiveDirectoryList
# Decode blob URL and download file
# https://github.com/BtbN/FFmpeg-Builds/releases/latest
# -y: overwrite output files
# -bsf bitstream_filters: a comma-separated list of bitstream filters
# -vcodec codec: force video codec ('copy' to copy stream)
ffmpeg -i "URL.m3u8" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 D:\video.mkv -y
# Change brightness to 100%
(Get-WmiObject -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods).WmiSetBrightness(1,100)
# Update Local Group Policy Editor (gpedit.msc) to make all manually created policy keys in the registry visible in the snap-in
# https://www.brookspeppin.com/2018/11/04/modify-local-gpo-examples/
LGPO.exe /b C:\Temp /n "Backup"
LGPO.exe /parse /m C:\Temp\LGPO_Backup\DomainSysvol\GPO\Machine\registry.pol >> C:\Temp\lgpo.txt
LGPO.exe /t C:\Temp\lgpo.txt
# TrueForAll
[array]::TrueForAll(
[string[]](Get-Service -Name WinDefend, SecurityHealthService, wisvc).Status,
[Predicate[string]]{
param ($Status)
$Status -eq "Running"
}
)
# Download Windows.winmd from Windows 10 SDK
# https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/
$Parameters = @{
Uri = "https://software-download.microsoft.com/download/pr/19041.685.201201-2105.vb_release_svc_prod1_WindowsSDK.iso"
OutFile = "$PSScriptRoot\WindowsSDK.iso"
UseBasicParsing = $true
}
Invoke-RestMethod @Parameters
# Mount ISO
$Mount = Mount-DiskImage -ImagePath "$PSScriptRoot\WindowsSDK.iso" -PassThru
$DriveLetter = ($Mount | Get-Volume).DriveLetter
# Install "Windows SDK for UWP Managed Apps" only
Start-Process -FilePath ($DriveLetter + ":\" + "WinSDKSetup.exe") @("/features", "OptionId.UWPManaged", "/quiet", "/norestart") -Wait
# Unmount ISO
Dismount-DiskImage -ImagePath "$PSScriptRoot\WindowsSDK.iso"
# Auto elevate script
$IsAdmin = ((New-Object -TypeName System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
if (-not $IsAdmin)
{
Start-Process -FilePath powershell.exe -ArgumentList "-ExecutionPolicy Bypass -NoProfile -NoLogo -File `"$PSCommandPath`"" -Verb Runas
exit
}
& "$PSScriptRoot\File.ps1"
# Check Internet connection. Works even if ICMP echo is disabled
# https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
try
{
$Parameters = @{
Name = "dns.msftncsi.com"
Server = "1.1.1.1"
DnsOnly = $true
ErrorAction = "Stop"
}
if ((Resolve-DnsName @Parameters).IPAddress -notcontains "131.107.255.255")
{
return
}
}
catch [System.ComponentModel.Win32Exception] {}
# Remove string in file by its' number
# Count starts from 1
$Number = 5
Get-Item -Path "D:\1.txt" | ForEach-Object -Process {
(Get-Content -Path $_ -Encoding UTF8) | Where-Object -FilterScript {$_.ReadCount -ne $Number} | Set-Content -Path $_ -Encoding UTF8 -Force
}
# Replace string in file by its' number or other criteria
Get-Content -Path "D:\1.txt" -Encoding UTF8 | ForEach-Object -Process {
if ($_.StartsWith("UpdateDefender")) # $_.ReadCount -ne X
{
$_ -replace "text1","text2"
}
else
{
$_
}
} | Set-Content -Path "D:\1.txt" -Encoding UTF8 -Force
# Remove first X strings in file
$File = Get-Content -Path D:\1.txt -Encoding UTF8
$LineNumber = ($File | Select-String -Pattern "Pattern").LineNumber
$x[($LineNumber-1)..($File.Count)] | Set-Content -Path D:\1.txt -Encoding UTF8 -Force
# Remove string in file
$hosts = Get-Content -Path D:\1.txt -Encoding UTF8 -Force
$hosts | ForEach-Object -Process {
$hosts = $hosts | Where-Object -FilterScript {$_ -notmatch "text_to_remove"}
}
$hosts | Set-Content -Path D:\1.txt -Encoding UTF8 -Force
# Add a string to a file in particular line
$Line = 27
$String = "text"
$file = (Get-Content -Path D:\1.txt -Encoding UTF8 -Force) -as [Collections.ArrayList]
$file.Insert($Line, $String)
$file | Set-Content -Path D:\1.txt -Encoding UTF8 -Force
# Remove string in file by text in it
(Get-Content -Path "D:\1.txt") | Where-Object -FilterScript {$_ -notmatch "text"} | Set-Content -Path "D:\1.txt" -Force
# Add dot at every string end in JSON file
$json = Get-Content -Path D:\File.json -Encoding UTF8 | ConvertFrom-Json
$json.Arg.Zero | ForEach-Object -Process {
# Check if $_.ToolTip exists for each level before proceeding
# We need to call the value we want to change ($_.ToolTip). We cannot just call $_
if ($_.ToolTip)
{
if ((-not $_.ToolTip.EndsWith(".")) -and ($_.ToolTip -ne ""))
{
$_.ToolTip = $_.ToolTip | ForEach-Object -Process {$_.Substring(0, $_.Length) + "."}
}
}
}
$json | ConvertTo-Json -Depth 32 | Set-Content D:\File.json -Encoding UTF8 -Force
# Add dot at every string end in txt file
Get-Content -Path D:\file.txt -Encoding Default | ForEach-Object -Process {
if (($_ -ne "") -and ($_ -notmatch "ConvertFrom-StringData") -and ($_ -ne "'@"))
{
if ($_.EndsWith("?") -or $_.EndsWith("."))
{
$_
}
else
{
$_ | ForEach-Object -Process {$_.Substring(0, $_.Length) + "."}
}
}
} | Set-Content -Path D:\file.txt -Encoding Default -Force
# Get sub keys' value kind recursively
(Get-Item -Path "HKCU:\Control Panel\Cursors").Property.Split([System.Environment]::NewLine) | ForEach-Object -Process {
(Get-Item -Path "HKCU:\Control Panel\Cursors").GetValueKind($_)
}
# Close a process by its' argument
$gpedit_Process_ID = (Get-CimInstance -ClassName CIM_Process | Where-Object -FilterScript {$_.Name -eq "mmc.exe"} | Where-Object -FilterScript {
$_.CommandLine -match "GPEDIT.MSC"
}).Handle
Get-Process -Id $gpedit_Process_ID | Stop-Process -Force
# https://stackoverflow.com/a/57134096/8315671
$OutputEncoding = [System.Console]::InputEncoding = [System.Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($true)
# Expand %TEMP% path
[Environment]::GetEnvironmentVariable("TEMP", "User")
Get-ItemPropertyValue -Path HKCU:\Environment -Name TEMP
(Get-Item -Path $env:TEMP).FullName
[System.IO.Path]::GetTempPath()
# Extract strings from mui files using Resource Hacker
# http://www.angusj.com/resourcehacker
Get-ChildItem -Path D:\Downloads\LanguagePack -Recurse -Force -Filter *.mui -File | ForEach-Object -Process {
Write-Verbose -Message $_.FullName -Verbose
$Name = $_.Name.Replace("$($_.Extension)", "")
Start-Process -FilePath "D:\ResourceHacker.exe" -ArgumentList @("-open", $_.FullName, "-save", "D:\extract\$Name.rc", "-action extract", "-mask MESSAGETABLE", "-mask STRINGTABLE") -Wait