PowerShell is an object-oriented software engine and a command-line scripting language that provides IT professionals with greater opportunities to configure the operating systems of the MS Windows family. Simply put, it is a kind of universal administration tool. This article will discuss the basic techniques for writing scripts on PowerShell, allowing you to automate the management of your Windows environment in a simple way.
PowerShell offers both a pure console interface and a full-fledged PowerShell ISE (Integrated Scripting Environment) integrated scripting environment. To launch the command line interface, enter powershell in the Run menu (WinKey + R). PowerShell ISE is launched using the “PowerShell ISE” command in the same menu.
ISE is more preferable, because it provides more opportunities for the developer due to syntax highlighting, code auto-completion function and other features inherent in many “big” IDEs.
The following PowerShell script recipe will help you delete a remote file based on a list of computers stored in a text file. New PowerShell function will be created during the session which will be piped from the text file.
Using the keyboard, you should quickly launch "Windows Explorer." You just need to press the keys Win + E and "Explorer" will be in front of your eyes.
Create new file directory C:\Scripts\ and file named Active_Computers.txt in this folder and populate the computer names.
Click Start, type PowerShell, and then click Windows PowerShell. If Windows 10 is installed on your computer, then perhaps an even faster way to open PowerShell is to right-click on the “Start” button and select the desired menu item (there are two items at once - for easy launch and on behalf of the administrator). The same menu can be accessed by pressing the Win + X keys on the keyboard.
function delete-remotefile {
- PROCESS {
- $file = "\\$_\c$\install.exe"
- if (test-path $file)
- {
- echo "$_ install.exe exists"
- Remove-Item $file -force
- echo "$_ install.exe file deleted"
- }
- }
- }
- Get-Content C:\Scripts\Active_Computers.txt | delete-remotefile.
$Computerlist = get-content C:\XXXXXXXX\Servers.txt
- Foreach ($computer in $Computerlist) {
- Get–ChildItem –Path \\$computer\c$\XXXXXXX\*.* –Include *.* –Recurse –Force | Remove–Item –Force
- {
Also consider using Action1 to delete files remotely if: