4.3 KiB
Working with Files and Folders
Get-Help
Get-Children
Gets items in a specified location. To list the folders in my drive C, I will run the command below:
Get-ChildItem c:\
This will list all the top-level folders. To list all files, folders include sub-folders use the -Recurse
parameter.
Copy-Item and Move-Item
You could use the Get-ChildItem Cmdlet to list items in a folder, then pipe the result to Copy-Item Cmdlet to copy the items to a new location. The command below will do the job:
Get-ChildItem C:\Dropbox | Copy-Item -Destination C:\NewFolder
The above PowerShell command will only copy the top-level folders and files - it will NOT copy sub-folders and files. To copy all files and folders including sub-folders, include the -Recurse parameter in the Get-ChildItem command as shown below:
Get-ChildItem C:\Dropbox -Recurse | Copy-Item -Destination C:\NewFolder
While the Copy-Item Cmdlet copies items from one location to another the Move-Item Cmdlet moves the item.
RemoveItem
NewItem
New-Item can be used to create files, folders and registry keys and entries. The command below creates a text file called weekly_file.txt in c:\logfiles folder:
New-Item -Path c:\logfiles -Name weekly_file.tx
RenameItem
Rename-Item Cmdlet is used to rename things in Windows. This Cmdlet can rename files, folders and registry keys. This command will rename weekly_file.txt to monthly_file.txt
Rename-Item -Path C:\logfiles\weekly_file.txt -NewName monthly_file.txt
Export-Csv
Export-Csv converts a set of string into CSV and saves in a file. This Cmdlet is very important in reporting.
Get-Command -Verb Export
Get-Command -Verb Export | Select-Object CommandType, Name, Version, Source | Export-Csv -
NoTypeInformation -Path C:\NewFolder\ExportCommands.CSV
Managing Processes
Get-Process
This PowerShell Cmdlet lists all the processes running on a local computer. If you use the ComputerName parameter, you can display the processes on a remote computer.
Start-Process and Stop-Process
The Start-Process Cmdlet can start a stopped process while the Stop-Process Cmdlet can stop a running process. To start a process, pipe the output of Get-Process command to the Start-Process command. As an example, to stop a process with ID 10500, use the command below.
Get-Process -Id 10500 | Stop-Process
Getting Computer Information
Get-ComputerInfo
Get-WmiObject
Get-WmiObject has a parameter called -Class this allows you to specify the WMI object you wish to access. The command below will get a list of WMI classes, Get-WmiObject -List -Class Win32*
Class List
- Win32_SystemBIOS
- Win32_Processor
- Win32_OperatingSystem
- Win32_Fan
- Win32_BIOS
- Win32_Account
Once you know the name of the WMI class, you can execute Get-WmiObject to return useful information from a local or remote computer. Below is a list of the most important WMI classes you may need:
- Win32_PhysicalMemory - information about available memory
- Win32_Processor - Processor information
- Win32_LogicalDisk - Logical disk drive information
- Win32_DiskDrive - Physical disk information
- Win32_OperatingSystem - Information about the operating system
To get information about the operating system, run the command below:
Get-WmiObject -Class Win32_OperatingSystem
Get-CimInstance
Get-CimInstance -ClassName CIM_Processor | Where-Object {$_.'DeviceID' -eq 'CPU0'} | ft -HideTableHeaders
To extract CPU information only
Get-CimInstance -ClassName CIM_Processor | Select Caption | ft -HideTableHeaders
To extract CPU name only
Get-CimInstance -ClassName CIM_Processor | Select Name | ft -HideTableHeaders
To extract CPU manufacturer only
Get-CimInstance -ClassName CIM_Processor | Select Manufacturer | ft -HideTableHeaders
Cim-Class List
To get full list of CimClass, run Get-CimClass
- Win32_SystemBIOS
- Win32_DiskPartition
- Win32_Processor
- CIM_Processor (DeviceID, Name, Caption, MaxClockSpeed, Manufacturer)
SYSTEMINFO
SYSTEMINFO displays operating system configuration information for a local or remote computer.
SYSTEMINFO /FO LIST
DRIVEQUERY
__ DRIVERQUERY__ displayS a list of installed device drivers on a local or remote computer.
DRIVEQUERY /FO TABLE