Here is a memento of several useful commands in PowerShell (check files, folders and registry, create or delete files and folders, run executables with or without arguments, displaying MsgBox…).
Here is the TechNet listing the standard CmdLets: https://technet.microsoft.com/en-us/library/hh848794.aspx
There are only few commands for the time being, but this article will be updated progressively.
.
Check Files, Folders and Registry
Cmdlet: Test-Path
## Check if a folder exists Test-Path C:\Workdir ## Check if a file exists Test-Path C:\Workdir\test.txt ## Check if there is any txt file in C:\Workdir Test-Path C:\Workdir\*.txt ## Check if registry exist Test-Path HKLM:\Software\Microsoft\Windows\CurrentVersion
Create Files and Folders
Cmdlet: New-Item
## Create a folder New-Item C:\Workdir\Scripts -type directory ## Create a file New-Item C:\Workdir\Scripts\new_file.txt -type file ## Force to create a new file and overwrite if already exist New-Item C:\Workdir\Scripts\new_file.txt -type file -force
Delete Files and Folders
Cmdlet: Remove-Item
## Remove a file Remove-Item C:\Workdir\test.txt ## Remove all files in a folder Remove-Item C:\Workdir\* ## Remove all txt files from a folder Remove-Item C:\Workdir\*.txt ## Remove a folder with sub-folders and files without confirmation Remove-Item C:\Workdir -recurse
Run Executables
## Run an exe file in the same folder as the script & .\setup.exe ## Run an exe file using an absolute path & "C:\Workdir\setup.exe" ## Run an exe file with arguments $EXE = 'setup.exe' $ARG = '-silent' & $EXE $ARG
Display MsgBox
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [System.Windows.Forms.MessageBox]::Show("Hello Guys !" , "Info")
Properties
# Get all information Write-Host "Get all information" Get-Host # Get only the "Name" property Write-Host "Get only the Name property" Get-Host | Select Name # Get the "Name" property as a string Write-Host "Get the Name property as a string" Get-Host | % {$_.Name} # Same thing with another method Write-Host "Same thing with another method" (Get-Host).Name