powershell commands

965
powershell commands

Here we can see, “powershell commands”

PowerShell is an object-oriented automation engine and scripting language with an interactive command-line shell that Microsoft developed to assist IT professionals configure systems and automate administrative tasks.

PowerShell, built on the .NET framework, works with objects, whereas most command-line shells are supported text. PowerShell is an automation tool for system administrators employed in IT departments and external entities, like managed service providers, due to its scripting capabilities.

PowerShell originated as a propriety offering that was only available on Windows. In 2016, Microsoft open-sourced PowerShell and made it available on Linux and macOS.

How to launch PowerShell

9 ways to launch PowerShell in Windows (including as admin)

Many IT professionals use PowerShell to manage Windows computers and devices and execute administrative tasks of all types. However, before you use PowerShell, you initially got to skills to start it. That’s why we made an extended list of all the methods available for starting this tool, including admin rights. Read on and see them all:

1. Start PowerShell using search (all Windows versions)

In Windows 10, one of the fastest ways to launch PowerShell is to use search. Inside the search field from the taskbar, enter Powershell. Then, click or tap the Windows PowerShell result.

If you would like to run PowerShell as administrator, right-click (or tap and hold, if you employ a touchscreen) on the Windows PowerShell search result, then click or tap “Run as administrator.”

If you employ Windows 7, open the beginning Menu, type PowerShell within the search field, then press Enter or click the Windows PowerShell result.

When you want to run PowerShell as administrator, right-click on the Windows PowerShell search result and choose “Run as administrator.”

In Windows 8.1, switch to the beginning screen and begin typing Powershell. Then, once the search results are displayed, press Enter on your keyboard or press the Windows PowerShell result.

Also See:  The Giant Infernape Glitch in the Pokémon Brilliant Diamond Battle Is Wild

When you want to run PowerShell as administrator, right-click on the Windows PowerShell search result and choose “Run as administrator” within the menu that’s displayed.

2. Start the PowerShell using the Run window (all Windows versions)

One of the quickest ways to start PowerShell in any modern version of Windows is to use the Run window. A quick thanks to launching this window is to press the Win + R keys on your keyboard. Then, type PowerShell and press Enter or click OK.

3. Launch the PowerShell from the Start Menu (in Windows 10 and Windows 7) or the Start Screen (in Windows 8.1)

In Windows 10, open the beginning Menu and attend the Windows PowerShell shortcuts folder. There, you’ll find a shortcut for Windows PowerShell.

If you employ Windows 7, open the beginning Menu and attend All Programs. Then, open Accessories, then the Windows PowerShell folder. There, click the Windows PowerShell shortcut.

In Windows 8.1, attend the beginning screen. Next, open the Apps View and scroll right until you discover the Windows System folder. There, click or tap the Windows PowerShell shortcut.

Note: To run it as administrator, right-click on the Windows PowerShell shortcut, then click the “Run as administrator” option.

4. Run PowerShell using its executable file (all Windows versions)

You can also open File Explorer (in Windows 10 and Windows 8.1) or Windows Explorer (in Windows 7). If you’re employing a 32-bit version of Windows, browse to: “C:\Windows\System32\WindowsPowerShell\v1.0.” There you discover the powershell.exe file on 32-bits.

If you employ a 64-bit version of Windows, browse to: “C:\Windows\SysWOW64\WindowsPowerShell\v1.0.” There you discover the powershell.exe file on 64-bits.

5. Start PowerShell using the WinX power user menu (Windows 10 only)

Windows 10 includes a hidden menu for power users named the WinX menu. The quickest thanks to launching it’s to press the Win + X keys on your keyboard, but you’ll also roll in the hay with right-click (or tap and hold) on the Windows logo from the bottom-left corner of your desktop. Here you discover shortcuts for starting PowerShell, both with limited permissions and with admin rights.

Use the Windows PowerShell shortcut you favour for starting it.

6. Create a shortcut for PowerShell on your desktop (all Windows versions)

If you favour using shortcuts, then don’t hesitate to make a shortcut to PowerShell. However, if you are not skilled and wish help with creating shortcuts, read this guide: the way to create shortcuts for apps, files, folders and sites in Windows. the sole thing you’ve got to recollect is to type PowerShell because of the location of the item you create the shortcut for, a bit like within the screenshot below.

You can also use the direct path to the powershell.exe file, shared at method four from this guide.

7. Use Task Manager to start PowerShell (all Windows versions)

Another way of opening PowerShell is to use the Task Manager:

  1. Launch Task Manager: a fast thanks to roll in the hay is to press the Ctrl + Shift + Esc keys on your keyboard.
  2. If you’re using Windows 10 or Windows 8.1 and therefore the Task Manager exposes in its compact mode, click or tap on “More details.”
  3. Altogether Windows versions, open the File menu and click on or tap on “Run new task.” within the “Create new task” window, type PowerShell and press Enter or OK.

8. Open PowerShell by using the shortcut from our collection (all Windows versions)

We assembled an in-depth collection of shortcuts for Windows. Download it, extract it, and you’ll find the Windows PowerShell shortcut within the Windows PowerShell subfolder for your Windows version.

9. Open PowerShell from the Command Prompt (all Windows versions)

A geeky method that works involves starting PowerShell from the prompt. If you’ve opened the prompt, type “start powershell” (without the quotation marks) and press Enter on your keyboard.

If you would like to start PowerShell from the prompt as administrator, confirm that you first open the prompt as administrator.

Following is a list of important PowerShell Commands:

Basic PowerShell Commands

Get Execution Policy

get-executionpolicy

Set Execution Policy to Unrestricted

set-executionpolicy unrestricted

Show PowerShell Version

$PSVersionTable

Get help for a command

Use this to urge the assistance information for a command.

get-help command-name

Search Get Help

Use this to look at the assistance files. This is often useful if you don’t know the command or want to ascertain if one exists.

get-help *keyword*
Also See:  iPhone 13 Pro 120Hz ProMotion display takes scrolling speed into account

Get Installed Modules

Use this command to display all the installed modules on a computer.

get-installedmodule

List All Available Modules

This will list all available modules on the pc.

Get-Module -ListAvailable

Exporting results to CSV

Add export-CSV to the top of commands

Get-ADUser username -Properties * | Select name, department, title | export-csv c:user.csv

Display available commands

This will display all commands that are available supported the modules that are loaded.

get-command

Find New Modules

Replace *NTFS* with the keyword you would like to look for. For example, this searches modules at https://www.powershellgallery.com/.

Find-Module *ntfs*

Install a New Module

Installs modules from https://www.powershellgallery.com/

I found a module called NTFSSecurity. To put in it, I run this command.

install-module NTFSSecurity

Windows Server & Client Commands

Get all Services

get-service

Get all Processes

get-process

Display Network Adapters

Gets detailed about the network adapter installed like name, status, speed and mac address.

get-netadapater

Restart Remote Computers

Restart-Computer -ComputerName "Server01", "Server02", "localhost"

Get the Last Boot Time

This takes a couple of lines

$os = Get-WmiObject win32_operatingsystem $uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime) Write-Output ("Last boot: " + $os.ConvertToDateTime($os.LastBootUpTime))

You can also run this single line to urge last boot time.

systeminfo | more

Start a Remote Session

Use this to start an interactive session with a foreign computer.

Enter-PSSession -ComputerName

Read the Content of a File (Open a file)

This example shows the way to read the content of the windows firewall log file.

Get-Content -Path "c:windowssystem32logfilesfirewallpfirewall.log"

Copy Files & Folders

Use this command to repeat a whole folder to a different folder. This may copy the folder and every one the subfolder/files. The -verbose command will display the results to the console.

copy-item E:\WindowsImageBackup\exchange -destination \\server1\Backups\Exchange -recurse -verbose

Active Directory PowerShell Commands

View all Active Directory commands

get-command -Module ActiveDirectory

Display Basic Domain Information

Get-ADDomain

Get all Domain Controllers by Hostname and Operating

Get-ADDomainController -filter * | select hostname, operatingsystem

Get all Fine Grained Password Policies

Get-ADFineGrainedPasswordPolicy -filter *

Get Domain Default Password Policy

Gets the password policy from the logged-in domain

Get-ADDefaultDomainPasswordPolicy
Also See:  chassis intruded fatal error system halted

Backup Active Directory System State Remotely

This will copy the domain controllers system state data. Change DC-Name to your server name and alter the Backup-Path. The backup path are often an area disk or a UNC path.

invoke-command -ComputerName DC-Name -scriptblock {wbadmin start systemstateback up -backupTarget:"Backup-Path" -quiet}

AD User PowerShell Commands

This section is all Active Directory user commands.

Get User and List All Properties (attributes)

Change username to the samAccountName of the account

Get-ADUser username -Properties *

Get User and List Specific Properties

Just add whatever you would like to display after select.

Get-ADUser username -Properties * | Select name, department, title

Get All Active Directory Users in Domain

Get-ADUser -Filter *

Get All Users From a Specific OU

OU = the distinguished path of the OU

Get-ADUser -SearchBase “OU=ADPRO Users,dc=ad,dc=activedirectorypro.com” -Filter *

Get AD Users by Name

This command will find all users that have the word Robert within the name. Just change Robert to the word you would like to look for.

get-Aduser -Filter {name -like "*robert*"}

Get All Disable User Accounts

Search-ADAccount -AccountDisabled | select name

Disable User Account

Disable-ADAccount -Identity rallen

Enable User Account

Enable-ADAccount -Identity rallen

Get All Accounts with Password Set to Never Expire

get-aduser -filter * -properties Name, PasswordNeverExpires | where {$_.passwordNeverExpires -eq "true" } | Select-Object DistinguishedName,Name,Enabled

Find All Locked User Accounts

Search-ADAccount -LockedOut

Unlock User Account

Unlock-ADAccount –Identity john.smith

List all Disabled User Accounts

Search-ADAccount -AccountDisabled

Force Password Change at Next Login

Set-ADUser -Identity username -ChangePasswordAtLogon $true

Move a Single User to a New OU

You will need the distinguishedName of the user and, therefore, the target OU

Move-ADObject -Identity "CN=Test User (0001),OU=ADPRO Users,DC=ad,DC=activedirectorypro,DC=com" -TargetPath "OU=HR,OU=ADPRO Users,DC=ad,DC=activedirectorypro,DC=com"

Move Users to an OU from a CSV

Setup a CSV with a reputation field and an inventory of the users sAmAccountNames. Then change the target OU path.

# Specify target OU. $TargetOU = "OU=HR,OU=ADPRO Users,DC=ad,DC=activedirectorypro,DC=com" # Read user sAMAccountNames from csv file (field labeled "Name"). Import-Csv -Path Users.csv | ForEach-Object { # Retrieve DN of User. $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName # Move user to target OU. Move-ADObject -Identity $UserDN -TargetPath $TargetOU }

AD Group Commands

Get All members Of A Security group

Get-ADGroupMember -identity “HR Full”

Get All Security Groups

This will list all security groups during a domain.

Get-ADGroup -filter *

Add User to Group

Change group-name to the AD group you want to add users to

Add-ADGroupMember -Identity group-name -Members Sser1, user2

Export Users From a Group

This will export group members to a CSV, change group-name to the group you want to export.

Get-ADGroupMember -identity “Group-name” | select name | Export-csv -path C:OutputGroupmembers.csv -NoTypeInformation

Get Group by keyword

Find a group by keyword. Helpful if you are not sure of the name, change group-name.

get-adgroup -filter * | Where-Object {$_.name -like "*group-name*"}

Import a List of Users to a Group

$members = Import-CSV c:itadd-to-group.csv | Select-Object -ExpandProperty samaccountname Add-ADGroupMember -Identity hr-n-drive-rw -Members $members

AD Computer Commands

Get All Computers

This will list all computers in the domain

Get-AdComputer -filter *

Get All Computers by Name

This will list all the computers in the domain and only display the hostname

Get-ADComputer -filter * | select name

Get All Computers from an OU

Get-ADComputer -SearchBase "OU=DN" -Filter *

Get a Count of All Computers in Domain

Get-ADComputer -filter * | measure

Get all Windows 10 Computers

Change Windows 10 to any OS you want to search for

Get-ADComputer -filter {OperatingSystem -Like '*Windows 10*'} -property * | select name, operatingsystem

Get a Count of All computers by Operating System

This will provide a count of all computers and group them by the operating system. A great command to give you a quick inventory of computers in AD.

Get-ADComputer -Filter "name -like '*'" -Properties operatingSystem | group -Property operatingSystem | Select Name,Count

Delete a single Computer

Remove-ADComputer -Identity "USER04-SRV4"

Delete a List of Computer Accounts

Add the hostnames to a text file and run the command below.

Get-Content -Path C:ComputerList.txt | Remove-ADComputer

Delete Computers From an OU

Get-ADComputer -SearchBase "OU=DN" -Filter * | Remote-ADComputer

Group Policy Section

Get all GPO related commands

get-command -Module grouppolicy

Get all GPOs by status

get-GPO -all | select DisplayName, gpostatus

Backup all GPOs in the Domain

Backup-Gpo -All -Path E:GPObackup

Office 365 PowerShell Commands

Connect To Exchange Online

This will pop up and ask for credentials

$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection Import-PSSession $Session

Force Azure Sync

This is for the azure ad sync client.

Force delta sync (only sync changes

Start-ADSyncSyncCycle -PolicyType Delta Force a full sync Start-ADSyncSyncCycle -PolicyType Initial

Get A List of All Office 365 Users

Get-MsolUser | Select DisplayName, City, Department, ObjectID

Get Full mailbox details

Get-Mailbox email-address | fl

Get Calendar Permissions

Get-MailboxFolderPermission username:calendar

Enable Remote Mailbox (Hybrid Environment)

Use this command if you have an existing on-premise user that needs an office 365 mailbox. There are other ways to do this but this creates all the attributes in the AD account.

Replace the username and the tenant fields

Enable-RemoteMailbox username -RemoteRoutingAddress "username@tenant.mail.onmicrosoft.com"
Also See:  Solarwinds Error: Unable to Connect to Server

User Questions:

1.What can you do with PowerShell?

The uses of PowerShell include adding and deleting accounts, editing groups, and creating listings to view specific types of users or groups. You can also choose to use the Windows PowerShell Integrated Scripting Environment (ISE), a graphic user interface that lets you run commands and create or test scripts.

2.Which is better cmd or PowerShell?

PowerShell is a more advanced version of the cmd used to run external programs like ping or copy and automate many different system administration tasks which are not accessible from cmd.exe. It’s quite similar to cmd except it’s more powerful and uses different commands altogether.

3.Why is PowerShell popping up?

The reason PowerShell open on Startup is likely because you mistakenly added Windows PowerShell shortcut to the Start-up folder. If you also look at the Start-up tab of Task Manager, Windows PowerShell will be listed and status displayed as Enabled. Moreover, there may be other reasons PowerShell may open on startup.

4.Windows PowerShell Scripting Tutorial for Beginners

Windows PowerShell Scripting Tutorial for Beginners from Netwrix

5.The only PowerShell Command you will ever need to find out who did what in Active Directory

The only PowerShell Command you will ever need to find out who did what in Active Directory from sysadmin

Table of Contents