Powershell is an object based distributed automation engine and scripting language. PowerShell is object based because it deals with objects. It is very useful for SQL Server DBAs, Windows administrators and other power users to automate the processes and configurations using command line commands. The operations in the command line are executed by cmdlets. Cmdlets are .NET classes to implement operations in the PowerShell.
Here are the most frequently asked powershell interview questions with answers.
- What is PowerShell?
Microsoft (MS) Windows PowerShell is an extendable command and scripting language for windows. It is based on object oriented standards and not text based but can only be used in Windows.
2. How do you find the installed PowerShell Version?
We use the following command
$PSVersionTable
Name Value
—- —–
PSVersion 5.1.19041.1320
PSEdition Desktop
3. What are cmdlets in PowerShell?
A cmdlet is a lightweight .NET framework class object that is invoked by the Powershell runtime.
4. What makes up variables in PowerShell?
Variables contain Objects, Integers and Strings.
5. When do you use pipeline symbol in PowerShell?
We use it when we have to join two statements.
6. What are ‘Select’ methods in PowerShell?
We use the select-object command to get properties from powershell output. Things are always fine with select-object if we select the single valued property, but if we select multi-valued property or grouped property, the powershell returns the result as comma-separated values, sometimes the value can not be readable format, in this case, we should use ExpandProperty to expand multi-valued (or grouped) property.
This example creates objects that have the Name, ID, and working set (WS) properties of process objects.
Get-Process | Select-Object -Property ProcessName, Id, WS
7. Can cmd (dos batch file) commands in PowerShell?
Yes. You can use it. PowerShell accepts all the cmd commands as legacy.
8. How do you call a function PowerShell?
We will declare a function using the function keyword. Then to call that function, just enter the function name.
For example in this code we have named the function name getipinfo
function getipinfo() {
// Code to get ip info
}
To pass arguments to the above function, we use the following syntax
function getipinfo($arg[0],arg[1]) {
}
9. What is the use of Get-Command in PowerShell?
The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other sessions
Get commands in the current session
This command uses the ListImported parameter to get only the commands in the current session.
Get-Command -ListImported
10. What is the difference between $Error and $ForEach variables?
$Error – It contains an array of error objects which represents most recent errors. The most recent error is first error in the error array.
$Foreach – The Foreach statement is also known as a Foreach loop, a language construct for stepping through (enumerating) a series of values in a collection of items.
In this example, the Foreach statement steps through the list of items that is returned by the Get-ChildItem cmdlet.
foreach ($file in Get-ChildItem) { Write-Host $file }
11. How do you use Arrays in PowerShell?
The array is a type of data structure that can be used to store a collection of items. The index of the array usually starts at 0. To access first element in an array we use index[0].
An array can be defined in the following ways.
@() can be used to create an empty array.
Example 1:
$sampledata =@()
$sampledata.count
Example 2:
$fruit = @(‘apple’,’orange’,’pear’)
$fruit[0] will return apple.
12. How do you convert the object into HTML?
The ConvertTo-Html cmdlet converts .NET frame objects into HTML that can be displayed in a web browser.
Example
Create a web page to display the date:
PS C:\> ConvertTo-Html -InputObject (Get-Date)
This command creates an HTML page that displays the properties of the current date. It uses the InputObject parameter to submit the results of a Get-Date command to the ConvertTo-Html cmdlet.
13. Can you tell me the most used commands in PowerShell?
Get-Help – This cmdlet is used to get help about any PowerShell cmdlet
Set-ExectionPolicy – This cmdlet is used to determine policies for running a PowerShell cmdlet or script
Get-ExecutionPolicy – This is used to know the current execution policy that is in place.
Get-Service – This cmdlet is used to list all the available services
Export-CSV – This is used to export the output of a cmdlet or script to a csv file.
Get-EventLog – It returns the events from the system event viewer.
Get-Process – It displays the set of processes that are currently running on the system.
Where-Object – It is used for filtering the data.
Get-Counter – This cmdlet is used to identify the various parameters that affect the performance of the system.
14. How do you locate all SQL Services in the Server?
Get-Service sql*
15. How do you execute SQL query in PowerShell?
Here is the command to execute SQL query.
Invoke-Sqlcmd -Query “SELECT GETDATE() AS Date;” -ServerInstance “MyComputer\MyInstance”
16. How to find all latest events that have an “error” in System log within the past 24 hours?
Below is the command to find all latest events with string error:-
Get-EventLog -LogName System -Message “error” -After $(Get-Date).AddHours(-24) -Newest 5
17. How are brackets used in PowerShell?
{} Braces Brackets for blocked statements
() Parentheses Brackets in compulsory arguments
18. In PowerShell what does variables hold?
In variables PowerShell contains strings, integers and objects. It does not have special variables as it is pre-defined with PowerShell.
19. What is the function of Get-ServiceStatus in PowerShell?
The cmdlets of windows enable you to filter the window services. PowerShell can list which services are ‘Running’ and which are ‘Stopped’ by scripting with windows.
20. How Do You Comment Out Code In Powershell?
Like other languages powershell also supports single/Inline comments line and multi line comments.
Starting with PowerShell V1 there’s only # to make the text after it a comment.
In PowerShell V2 “<# #>” can be used for block comments (multi-line) and more specifically for SYNOPSIS, DESCRIPTION, NOTES, LINK help comments.
Example: Sinlgle/Inline comments
# This is a single or inline comment starts with hash in Powershell
Example: Block comments (multi-line)
<# this is a first line of comment
this is a second line of comment
this is a third line of comment
.
.
this is a last line of comment
#>