Powershell Examples
Powershell Examples
You can also use wildcard '*' characters to remove multiple items. For example, this command removes all
the files in C:\test:
# Using PowerShell commands to delete all file
Remove-Item -Path "C:\test\*.*"
Here also you can use -Force command to delete files force fully
# Using PowerShell commands to delete all file force fully
Remove-Item -Path "C:\test\*.*" -Force
Here also you can use -Force command to delete files force fully
# Using PowerShell commands to delete all file and folders
Remove-Item -Path "C:\test\*.*" -recurse
how to Copy-files-specific-extension
Copy all files of a specific file extension to the desktop from given directory
Copy-Item $home\*.ps1 ([Environment]::GetFolderPath("Desktop"))
Each line of the file is an array element with the variable passed, so to display just the first line
you could use this:
$data = Get-Content "d:\projects\powershell\wakeup.dat"
$data[0]
If you used $data.count, it would show the number of lines in the file. You can also access each
line and perform some action by using the PowerShell foreach function, in this manner:
$data = Get-Content "d:\projects\powershell\wakeup.dat"
write-host $data.count total lines read from file
foreach ($line in $data)
{
write-host $line
}
Deleting Keys
Deleting items is essentially the same for all providers. The following commands will silently remove items:
Remove-Item -Path hkcu:\Software\_DeleteMe
Remove-Item -Path 'hkcu:\key with spaces in the name'
Working with Services
Listing the Serivices
Get-Service
Listing the Services on Remote machine
Get-Service -ComputerName Server01
Managing Processes
Getting Processes (Get-Process)
To get the processes running on the local computer, run a Get-Process with no parameters.
The following command gets process whose names begin with "ex."
PS> Get-Process -Name ex*
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ --------- ----- ------ -- ----------234
7 5572 12484 134 2.98 1684 EXCEL
555
15 34500
12384 134 105.25 728 explorer