loonison101
7/1/2013 - 1:15 AM

Usefull Powershell Commands

Usefull Powershell Commands

# Import all modules available on the system
Get-Module -ListAvailable | Import-Module

# Sort directory contents
Get-ChildItem -path . | sort-object lastwritetime -descending

# Start a new powershell prompt and give it a command to execute
Start-Process powershell "-noexit -command `"cd $place`""

# Run a cmd command and capture it's output
$a = ipconfig

# Find the error code of the last command that is external, greater than 0, not successful
rasdial.exe
$LastExitCode

# Find if the last cmd-let was successful
$?
 
# Access error objects
$error

# Match a string when filtering
get-childitem | where { $_.Name -match "visual"}

# Rename multiple files
get-childitem *.sql | rename-item -newname {$_.Name + "idk"}

# Move multiple files
move-item .\*.sql '..\other place\subdir'

# Open multiple files
get-childitem .\*.png |foreach-object {start-process $_.FullName -wait}

# Access computer remotely using PSSession
$creds = Get-Credential
$server = New-PSSession -ComputerName mycomputername -Credential $creds
Enter-PSSession $server

# Compress a file using 7zip - put anything for *.txt (selector)
7za a -t7z archiveName.7z *.txt

# Create new folder
new-item -type directory

# Expand a property name to show completely
get-service | select -expandProperty Name

# Start service on local machine
Get-Service -Name "Mongos" | Start-Service

# Start service on remote machine
Get-Service -Name "Mongos" -ComputerName loon-api | Set-Service -Status Running

# http://stackoverflow.com/questions/12644500/copy-item-is-does-not-retain-filename-or-extension-at-destination
copy-item C:\TEMP\installation.msi -Destination Q:\

# Access IIS features
Import-Module WebAdministration

# Create a web application in IIS
# http://www.iis.net/learn/manage/powershell/powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools
New-Item 'IIS:\Sites\Default Web Site\DemoApp' -physicalPath c:\test -type Application