Anda di halaman 1dari 5

Use Powershell to Delete File

Using PowerShell commands to delete a file


# Using PowerShell commands to delete a file
Remove-Item -Path "C:\test\FirstCreateFile.txt"

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 show message box from powershell?


[Reflection.Assembly]::LoadFrom("C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Windo
ws.dll")
[System.Windows.MessageBox]::Show("Record already exists.
Do you want to overwrite?", "Confirmation", "YesNo", "Question")

Copying Files and Folders Using Powershell


Copy-Item use to copy any file from one location to other.
Copy-Item -Path C:\fso\20110314.log -Destination c:\fsox\mylog.log
To copy the entire Directory and all of its content.
Copy-Item -Path C:\fso\*.* -Destination c:\fsox 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"))

Reading a Text File


Reading the content of a file in PowerShell is very easy. You use the Get-Content cmdlet and the
filename. Heres an example:
Get-Content "d:\projects\powershell\wakeup.dat"

To save the content to a variable just use this:


$data = Get-Content "d:\projects\powershell\wakeup.dat"

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
}

Writing to a Text File


$s=some text

$stream = [System.IO.StreamWriter] "t.txt"


1..10000 | % {
$stream.WriteLine($s)
}
$stream.close()

Reading a Registry Value


Get-ItemProperty Path <path to reg key> -Name <Value Name>
$val = Get-ItemProperty -Path hklm:software\microsoft\windows\currentversion\policies\system -Name
"EnableLUA"

Setting a Registry Value


Set-ItemProperty -Path hklm:software\microsoft\windows\currentversion\policies\system -Name
"EnableLUA" -Value 0

Renaming Registry Entries


To rename the PowerShellPath entry to "PSHome," use Rename-ItemProperty:
Rename-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PowerShellPath
-NewName PSHome

Listing All Subkeys of a Registry Key


Get-ChildItem -Path HKCU:\Software -Recurse

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

Stopping, Starting, Suspending, and Restarting Services


The Service cmdlets all have the same general form. Services can be specified by common name or display
name, and take lists and wildcards as values. To stop the print spooler, use:
Stop-Service -Name spooler
To start the print spooler after it is stopped, use:
Start-Service -Name spooler
To suspend the print spooler, use:
Suspend-Service -Name spooler
The Restart-Service cmdlet works in the same manner as the other Service cmdlets, but we will show some
more complex examples for it. In the simplest use, you specify the name of the service:
PS> Restart-Service -Name spooler
WARNING: Waiting for service 'Print Spooler (Spooler)' to finish starting...
WARNING: Waiting for service 'Print Spooler (Spooler)' to finish starting...
PS>
You will notice that you get a repeated warning message about the Print Spooler starting up. When you perform
a service operation that takes some time, Windows PowerShell will notify you that it is still attempting to
perform the task.

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

Stopping Processes (Stop-Process)


Windows PowerShell gives you flexibility for listing processes, but what about stopping a process?
The Stop-Process cmdlet takes a Name or Id to specify a process you want to stop. Your ability to stop
processes depends on your permissions. Some processes cannot be stopped. For example, if you try to stop the
idle process, you get an error:
PS> Stop-Process -Name Idle
Stop-Process : Process 'Idle (0)' cannot be stopped due to the following error:
Access is denied
At line:1 char:13
+ Stop-Process <<<< -Name Idle
You can also force prompting with the Confirm parameter. This parameter is particularly useful if you use a
wildcard when specifying the process name, because you may accidentally match some processes you do not
want to stop:
PS> Stop-Process -Name t*,e* -Confirm
Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "explorer (408)".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):n
Confirm

Are you sure you want to perform this action?


Performing operation "Stop-Process" on Target "taskmgr (4072)".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):n

Anda mungkin juga menyukai