View the content of a .bat file through command prompt
The Powershell script below gets the content of the file myfile.bat
, check if the string ipconfig
is present. If yes, the file is deleted.
$file = 'c:\temp\myfile.bat`
$a = Get-Content $file | Select-String ipconfig
if( $a.Length -gt 0){Remove-Item $file}
To include a folder for example your Desktop, change the script as shown below. Replace mthecodeexpert
with your real username.
$folder = 'c:\users\thecodeexpert\*.bat'
$files = Get-ChildItem $folder -file
$files | ForEach-Object{ $a = Select-String -Path $_.Fullname ipconfig; if ($a.Length -gt 0){Remove-Item $_.Fullname}}
Continuing from my comment...
# Find the files by type
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
# Results
<#
C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat
#>
# Test planned action on the file to find the string pattern
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName |
ForEach-Object {
If (Select-String -Path $PSItem -Pattern 'ipconfig')
{Remove-Item -Path $PSItem -WhatIf}
}
# Results
<#
What if: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
#>
# Take action on the file to find the string pattern
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName |
ForEach-Object {
If (Select-String -Path $PSItem -Pattern 'ipconfig')
{Remove-Item -Path $PSItem -Force -Verbose}
}
# Results
<#
VERBOSE: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\Desktop\SomeBatFile.bat".
#>
# Validate removal
(Get-ChildItem -Path "$env:USERPROFILE\Desktop" -Filter '*.bat' -Recurse).FullName
# Results
<#
No results
#>