Here are some sample PowerShell interview questions.
These are the kinds of questions that could be asked to gauge one's basic literacy and comfort level with PowerShell.
Basic literacy. Describe the following:
a. What is PowerShell? Why is it called a shell?
b. How does PowerShell differ previous Windows scripting techniques, such as CMD/batch files, VBScript, etc.?
c. What is a cmdlet?
d. What is meant when it's said that Powershell is "object-based"?
e. What is the pipeline? How is it useful?
Get the amount of free space on the C drive.
Ans:- Using WMI is an option
How to automate Internet Explorer to open a web site using PowerShell?
Please below three simple steps to open Internet Explorer a
Example
$ie = New-Object -ComObject InternetExplorer.Application
$ie.Navigate("http://www.google.com")
$ie.Visible = $true
How to run a PowerShell script?
Launch PowerShell
Navigate to the directory where the script lives
There is alternate way to run powershell script using "POWERSHELL_ISE".
Please see below option.
How to prepare window 7 bootable Disc using powershell?
Ans : Coming Soon..
Is PowerShell ready to replace my cygwin shell on Windows?
PowerShell is defiantly looks powerful tool for windows.
The person who worked in heterogeneous environments Linux, Solaris and Mac OS X and cygwin.
Can differentiate better way. It is nice and more powerful tool environment seems to have been thought out much more than Unix shells as it use of objects
instead of text streams.
Powershell is more powerful than the standard built-ins of the Unix shells.
Note: - Using objects is a paradigm shift as compare to other environments and it avoids the whole re-parsing at each step where structured data is
involved.
How to use powershell to download old version of package with nuget?
The powershell command Install-Package has a Version argument that can be used to specify a specific version.
Get-Package -ListAvailable -Filter Autofac -AllVersions
By pressing tab after the version option in the Install-Package command, you get a list of the latest available versions.
Install-Package Autofac -Version 1.2.0
Example
PS>cd D:\NineOptions\SSRSService\
PS>(dir -include *.cs,*.xaml,*.cpp,*.h,*.idl,*.asmx -recurse | select-string .).Count
PowerShell "execution of scripts is disabled on this system."
There two ways to solve the issue. As bydefault powershell -ExecutionPolicy is restricted.
You may change it to either of 4 available options
1. Restricted
2. Unrestricted
3. Signed
4. RemoteSigned
Set-ExecutionPolicy RemoteSigned
Or you can bypass the execution policy for particular ps1 file.
powershell -ExecutionPolicy ByPass -File script.ps1
PowerShell to build App.Config separately based on project.
You can get more detail about xmlpreprocess at below link.
Managing Properties
Create xml with each environment details.
A spreadsheet can be used to manage the properties for all of your environments in one place.
Command Line
Example
Usage: XmlPreprocess.exe [@argfile] [/input|i:] [/output|o:]
[/settings|s:] [/define|d:] [/clean|c] [/quiet|q] [/validate|v]
[/validateSettingsExist|vs] [/validateXmlWellFormed|vx] [/fixFalse|f]
[/environment|e:] [/spreadsheet|x:] [/database|db:]
[/custom|cx:] [/list|l] [/property|p:] [/delimiters|t:]
[/environmentRow|er:] [/firstValueRow|vr:]
[/settingNameCol|nc:] [/defaultValueCol|dc:]
[/environmentFile|ef:] [/noDirectives|n] [/tokenStart|ts:]
[/tokenEnd|te:] [/countReportFile|cr:] [/noLogo] [/help|?|h] [/version|v]
Powershell script for deploying configuration setting to actual web.config for each environment.
Example
foreach ($Env in $EnvList)
{
XmlPreprocess.exe /v /f /nologo /i "web.config" /s "SettingConfigs.xml" /e $remoteserver /o "$OutputPath\web.config"
}
Example
..
-ScriptBlock
${
$xArgvalue= arg[0]
$yArgvalue= arg[1]
function:Foo
} -ArgumentList $x,$y
..
Power shell is an extendable command shell and a scripting language for Windows.
The key characteristics of PowerShell are
PowerShell is object-based and not text based Commands in PowerShell are customizable It is a command line interpreter and scripting environment
In variables PowerShell contains strings, integers and objects. It does not have special variables as it is pre-defined with PowerShell
Parenthesis Brackets (): Curved parenthesis style brackets are used for compulsory arguments.
Braces Brackets {} : Curly brackets are employed in blocked statements
Square Brackets []: They define optional items, and they are not frequently used PowerShell
Cmdlet's are simple build in commands written in .net language like C# or VB introduced by Windows PowerShell
Automating repetitive task with the help of PowerShell loop is known as PowerShell Loop. Through PowerShell, you can execute For each loop, While loop and Do While loop.
If you have created a web part using VS 2010 then, you can deploy it using cntrl+f5. However to activate the web part feature you can write a PowerShell script (.ps1) and execute it after deployment
Comparison Operators compares value in PowerShell. Four types of comparison operators are used equality, match, containment and replace. In PowerShell, one of the key comparison operators is -eq that is used instead of "=" sign for declaring variables. Likewise, there are other operators like -ne for "not equal" , -gt ( greater than ) or -lt (less than).
PowerShell pipeline is used for joining two statements such that the output of one statement becomes the input of the second.
Get command in PowerShell is used to fetch other cmdlets, for example you are looking for cmdlet between letter L and R then your PowerShell get-command will be like
# PowerShell Get - Command Range
Clear-Host Get-Command [ L-R ]*
To map a network drive in PowerShell you have to use the command like
# PowerShell Map Network Drive
$Net = $( New - Object - ComObject Wscript.Network )
$Net.MapNetworkDrive( "S:", \\expert\admin )
In here the drive letter is "S:" and the Network share is called ‘expert' on a computer called ‘admin.'
The most common way is in a WMI Query Language (WQL) statement. In this technique Wmiobject uses ‘-query' to introduce a classic ‘Select * from' a phrase The second context for ‘Select' in PowerShell is Select-String. This cmdlet checks for a word, phrase or any pattern match Another way is Select Object
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.
PowerShell file contains a series of PowerShell commands with each command appearing on a separate line. To use a text file as a PowerShell script, its filename should have.PS1 extension. To run a script you need
Type the commands in a text editor Save the file with .ps1 extension Execute the file in PowerShell
A hash table is also referred as dictionary. It is an array that allows you to store data in a "key-value" pair association. The "key" and "value" can be of any data and length. To declare a hash table you have to use @ followed by curly braces.
The use of Array in PowerShell is to run a script against remote computers. In order to create an array, you have to create a variable and assign the array. Arrays are represented by "@"symbol, they are represented as hashtable but not followed by curly braces. For example, $arrmachine = @ ( "machine1" , "machine2" , "machine3")
Get-ChildItem C:\Scripts -recurse
To convert the object into HTML
Get-Process l Sort-object - property CPU -descending l convert to - HTML l Out-file "process.html"
To rename a variable,
Rename-Item- Path Env: MyVariable -NewName MyRenamedVar
The $input variable enables a function to access data coming from the pipeline
There are two ways to do this get-wmiobject win32_service l where-object {$_name-like "*sql*"} get-service sql*