You can use the Remove-Item cmdlet in PowerShell to delete specific folders and files.
Occasionally, when using the Remove-Item cmdlet you may be prompted by PowerShell to confirm that you actually want to delete specific items.
This particular example will delete the folder named current_data along with all files inside the folder without being prompted to confirm that you want to delete these items.
We can type ls to list out all files in this folder:

We can see that there are seven total files in this folder.
Suppose that we would like to delete this folder, including all of the files within it.

Notice that we are prompted by PowerShell to confirm that we would indeed like to delete this specific folder along with all of the files inside of it.

Notice that we aren’t prompted to confirm that we would like to delete this specific folder and its contents.
We can verify that the folder has been deleted by attempting to use the ls command again to view the contents of the folder:

We receive an error message stating that the path cannot be found. This confirms that we have successfully deleted the folder and its contents.
Note: You can find the complete documentation for the Remove-Item cmdlet in PowerShell here.
PowerShell: How to Delete File if it Exists
PowerShell: How to Delete All Files with Specific Extension
PowerShell: How to Delete Files Matching a Certain Pattern
Often you may want to use the Get-Content cmdlet in PowerShell to read the contents of a file but skip the first N lines.
This particular example will read all of the lines from the file located at the path we specified in $my_file but skip the first 3 lines.
Note: Feel free to replace 3 with whatever number you would like after the -Skip operator to instead skip a different number of lines at the beginning of the file.
Suppose we have a file named teams.txt that contains ten total lines with the names of various basketball teams.
We can use the Get-Content cmdlet to view all of the lines in this file:

Suppose that we would like to read all of the lines of this file but skip the first 3 lines.

Notice that we are able to read all of the lines in the file but skip the first 3 lines.
Note that if you would instead like to skip the last 3 lines then you could use the -SkipLast operator instead:

Notice that we are able to read all of the lines in the file but skip the last 3 lines.
Note: The -SkipLast operator is only available for PowerShell versions 5.0 or higher.
PowerShell: How to Get First Line of File
PowerShell: How to Get First Item in Array
PowerShell: How to Get First Character of String
Keyur Aghao
Domain Manager & Red Teamer @Bajaj Finance Ltd.
Through various Red Team assessment, I discovered one of the simplest way to bypass the Implemented Execution Policy.
This is extremely helpful as it allows the team to execute scripts and tools that might otherwise be restricted by security policies. This helps in simulating real-world attack scenarios more accurately, identifying security gaps, and testing the effectiveness of existing defenses. It ensures a comprehensive evaluation of an organization’s security posture, revealing potential vulnerabilities that could be exploited by malicious actors.
1. Access the Execution Context: It retrieves the private field _context from the $executioncontext object, which holds the current session’s execution context.
2. Get the Authorization Manager: It then retrieves the private field _authorizationManager from this context, which controls the execution policy.
3. Override the Authorization Manager: Finally, it sets this field to a new instance of AuthorizationManager, effectively bypassing any restrictions set by the original execution policy.
In short, this function disables the PowerShell execution policy by directly modifying the internal security context, allowing scripts to run without being restricted by the policy settings.
Please find the attached POC for reference.
Help improve contributions
Contribution hidden for you
Privacy is a Myth!!
Privacy is a Myth!!
99 followers

The ProblemJump To Solution
When I attempt to run a PowerShell script (for example, MyScript.ps1), I get the error:
Click to Copy
MyScript.ps1 cannot be loaded because the execution of scripts is disabled on this system.
How do I enable script execution?
The Solution
To run PowerShell scripts, it may be necessary to change the PowerShell execution policy. The execution policy on most modern Windows systems is set to Restricted by default, preventing the execution of any PowerShell scripts. This is a security measure as PowerShell scripts can have powerful and dangerous functionality.
Click to Copy
To temporarily bypass the execution policy and run scripts in a single PowerShell session, we can specify the flag -ExecutionPolicy Bypass when starting PowerShell from Windows CMD or the Run dialog.
For example, the command below will run MyScript.ps1 without modifying the execution policy:
Click to Copy
powershell -noexit -ExecutionPolicy Bypass -File MyScript.ps1
To enable script execution permanently, we can use PowerShell’s Set-ExecutionPolicy cmdlet.
Click to Copy
This will allow execution of any locally written PowerShell scripts, but will also require that PowerShell scripts downloaded from the internet are digitally signed by a trusted publisher. This is the default setting for Windows Servers.
If this policy is still too restrictive, we can remove all restrictions with this command:
Click to Copy
This will allow any script to be executed. You will still be prompted for permission when executing a script from the internet.
A full list of execution policy options is available here.
`[CmdletBinding()] param ()
try{ $query = 'C:\Queries\MissingDesignNumbersCheck.sql' $SQLConnection = Invoke-Sqlcmd2 -ServerInstance $SQLInstance -Database "$Database -InputFile $query -Credential $r_sa_pd -As DataSet $table = $SQLConnection.Tables[0] $count = $table.Rows.Count if (-not $count -eq 0){ $NextDesignNumbers = @() $FirstOnlyDesignNumbers = @() foreach ($row in $table.Rows){ $FirstOnlyDesignNumbers += $row.'First/Only Design Number' $NextDesignNumbers += [int]$row.'First/Only Design Number' + [int]$row.'Application Count' } $compare = Compare-Object -ReferenceObject $FirstOnlyDesignNumbers -DifferenceObject $NextDesignNumbers $InputObject = $compare | Where-Object SideIndicator -EQ '=>' $json = $InputObject.InputObject | ConvertTo-Json $json
}catch { Write-Error $_.Exception.Message }`If the for loop returns a value that is a string, not an int, I need to ignore it and move on to the next item in the for loop.
Currently an exception is thrown saying that it can’t handle a string and the whole function breaks.
[CmdletBinding()] param ()
try{ $query = 'C:\Queries\MissingDesignNumbersCheck.sql' $SQLConnection = Invoke-Sqlcmd2 -ServerInstance $SQLInstance -Database $Database -InputFile $query -Credential $r_sa_pd -As DataSet $table = $SQLConnection.Tables[0] $count = $table.Rows.Count if (-not $count -eq 0){ $NextDesignNumbers = @() $FirstOnlyDesignNumbers = @() foreach ($row in $table.Rows){ if($row -match "^[\d\.]+$"){ Continue } $FirstOnlyDesignNumbers += $row.'First/Only Design Number' $NextDesignNumbers += [int]$row.'First/Only Design Number' + [int]$row.'Application Count' } $compare = Compare-Object -ReferenceObject $FirstOnlyDesignNumbers -DifferenceObject $NextDesignNumbers $InputObject = $compare | Where-Object SideIndicator -EQ '=>' $json = $InputObject.InputObject | ConvertTo-Json $json }
}catch { Write-Error $_.Exception.Message }




