Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

January 28, 2020

Top 20 PowerShell Interview Questions & Answers


Ques: 1. What do you understand by PowerShell?

Ans: With PowerShell, you can control your system operations such as accessing data storage, registry, and file system. PowerShell is a command-line tool built upon the .NET framework to help the windows system administrator. The operations in the command line are executed by cmdlets. Cmdlets are .NET classes to implement operations in the PowerShell.

It also has a good expression parser and a scripting language to control and automate the administration of the windows system. This open-sourced tool was developed by Microsoft and released in 2006.




Ques: 2. What is $null in PowerShell?

Ans: The $null is a variable in the PowerShell that is used to represent NULL as the name suggests. It can be assigned to a variable, use it for comparisons, etc. In PowerShell, $null is an object that holds the value of NULL. To assign $null, use the following command

PS> $null –eq $variable
Here, the $variable holds the value of $null.


Ques: 3. Can you explain String Interpolation in PowerShell?

Ans: The String Interpolation is the way of displaying the value of the variable by surrounding the variable in the double-quotes.
//example

$var = “Nature”
Echo “the value is $var”

The above statement produces the ‘value is Nature’. Here the $var is surrounded by double quotes when used in the echo statement. So, it prints the value of the variable instead of the variable name itself. It is called string interpolation.


Ques: 4. What are cmdlet's in PowerShell?

Ans: The PowerShell cmdlet (Command let) is just a group of commands that are used in the PowerShell to perform a function. It is a lightweight .NET framework class object that is invoked by the PowerShell runtime. Programmers can create and invoke it manually too. You can construct your cmdlet by grouping a few lines of PowerShell code. The cmdlet has a .ps1 extension.


Ques: 5. How to call a function in powershell?

Ans: To call a function in PowerShell, first, you have to declare the function using the function keyword. Then to call the function, just type and enter the function name.
//example

function functionName {
$args[0] - $args[1]
}
PSC:\>functionName 10 5 5
You can also pass arguments to the called function as you see in the example above.


Ques: 6. What are Filters in PowerShell?

Ans: A lot of commands in the PowerShell return values that are not always useful. To get useful return objects from the commands we can use filters. Filters can be created using the Filter parameter or even using the Where-Object. We can use the Filter parameter with the Get-ChildItem to get the items that we specified in the filter.

//example
PS> Get-ChildItem -Path C:\folder\ -Filter '*1*.txt'

The above command filters and returns the files with a prefix as ‘1’. The same result can be achieved using the Where-Object, but generally, the filter is much faster than the Where-Object.


Ques: 7. What are $Home and $PID in PowerShell?

Ans: The $HOME is an automatic variable in the PowerShell. It contains the path of the user’s home directory. It is equivalent to C:\Users\. The $PID is also an automatic variable that contains the process identifier of the process which is hosting the PowerShell’s current session.


Ques: 8. Is PowerShell’s Execution Policy a security layer?

Ans: No. The execution policy in PowerShell is part of the security strategy but merely prevents potentially malicious scripts being executed by accident and, with the ‘ allsigned ‘ policy ensures that scripts cannot be altered without your knowledge. By default, this setting is set to ‘ Restricted ‘, meaning that no PowerShell script file can be run but it will execute PowerShell code within the PowerShell console.


Ques: 9. What is $Error and $ForEach variable?

Ans:

$Error – This variable contains an array of error objects which represents the most recent errors. The most recent error is the first error in the array.

$ForEach – This variable contains the enumerator (should not be confused with the resulting values) of a for each loop. Properties and methods of enumerators can be used on the value of the $ForEach variable. This kind of variable exists only while the for each loop is in running state, and it is deleted once the loop is completed.


Ques: 10. Explain Execution Policies and types of Execution Policy?

Ans: This is the common PowerShell Interview Questions asked in an interview. There are 6 types of execution policies in PowerShell. These are:-
  • Restricted: This is the default type. Under this, PowerShell will not run any script including PowerShell profiles too.
  • RemoteSigned: PowerShell will only run any script that is created locally. Any script that has been coming from the Internet should be digitally signed with a signing certificate code and is trusted by the computer.
  • AllSigned: PowerShell will only run any script that has been digitally signed using a trusted code signing certificate.
  • Unrestricted: PowerShell will run any script. If the script comes from an untrusted source, users are prompted once to execute it.
  • Bypass: This policy will run any script without any question or prompt.
  • Undefined: There is no execution policy set for this in the current scope.

Ques: 11. What is the PowerShell order in which execution policy is evaluated?

Ans: Windows PowerShell has execution policies in the following order of precedence: 
  • Group Policy: Example is Computer Configuration.
  • Group Policy: Example is User Configuration.
  • Execution Policy: Such as Process (or PowerShell.exe -Execution Policy) – which is the CURRENT SCOPE.
  • Execution Policy: Such as Current User – Which is SAVED in the HKCU registry.
  • Execution Policy: Such as Local Machine – which is SAVED in the HKLM registry.

Ques: 12. Can you explain the differences between the concept of WMI between old and new?

Ans:
Old WMI
  • Uses old-style native code providers and a repository for itself.
  • Available only on Windows as mentioned.
  • It has been more or less deprecated which means it’s is not focused on further improvement or development.
New WMI
  • Supports old-style native code providers and a repository, as well as new-style MI providers as discussed.
  • Available only on Windows as mentioned.
  • This is the way forward. It has an essentially stateless relationship with the remote machine.

Ques: 13. What are the various Differences between OMI and CIM.?

Ans:
OMI
  • It uses WS-MAN where OMI code includes the protocol stack od WS-MAN. It supports only new-style MI providers.
  • It is available on any implementing platform. If something can talk to OMI, it will be able to talk to NEW WMI as well.
CIM
  • It defines the standard. It is created by DMTF.
  • In this case, early versions were implemented as OLD WMI actually by Microsoft, but the newest version implemented both in NEW WMI and OMI by Microsoft as well as others.

Ques: 14. What are the differences among WinRM and WSMan and DCOM?

Ans:
 
WSMan: 
  • WS-Management abbreviated as WSMAN or Web Services-Management is basically a Distributed Management task force.
  • It is an open standard which defines a SOAP-based (full form Simple Object Access Protocol) protocol for the management of its servers, devices, applications and also various Web services.
WinRM:
  • WinRM is a feature which came from Windows Vista and it allows administrators to remotely run management scripts.
  • It can handle remote connections using the WS-Management Protocol.
DCOM:
  • DCOM means Distributed COM.
  • It is used to connect LIVE objects which are on the remote machine.
  • The RPC protocol that it uses was designed for continuous back-and-forth messaging.
  • It is network and memory inefficient.

Ques: 15. What are Scriptblocks?

Ans: Scriptblocks are one of most Powerful concept of PowerShell. By definition, is a collection of statements or expressions between “{” and “}” that can be used as a single unit. It can be stored in a variable, can be passed to function and can be executed remotely by the operator “&”

Syntax :
{<statement list>}
Like functions, a scriptblock can include parameters :
              {
            param ([type]$parameter1 [,[type]$parameter2])
           

And script blocks can include the DynamicParam, Begin, Process, and End keywords.

 
Ques: 16. What is Variable Interpolation?

Ans: When you add a variable to a double-quoted string, PowerShell replaces the variable name by its value. This feature is called variable interpolation.
              $HighOne = "Love"
"The most powerful Emotion is $HighOne" 

Output:

PS C:\Users\Administrator>
$HighOne = "Love"
"The most powerful Emotion is $HighOne"
"The most powerful Emotion is Love
PS C:\Users\Administrator>


Ques: 17. What is the {0} -f that I saw in some string codes?

Ans: This is string formatting using the PowerShell format operator. In PowerShell, you do not need to use “+” operator to concatenate strings. Actually is more in-tune with PowerShell syntax if you use interpolation or the -f (from format)

It is simple. You add the placeholder {N} and then type a command-separated list of values in order to be replaced. {0} represents the first value in the command-separated list, {1} the second and so on.


Ques: 18. What is a hash table in PowerShell?

Ans: Hash table is a data structure which used the mechanism of value/key pair. The professionals who create PowerShell scripts use variables to store data. For the storage of data in a highly secure environment hash table is used.


Ques: 19. How is Windows Powershell different from Stsadm ?
Ans: Unlike stsadm, which accept and return text, Windows PowerShell is built on the Microsoft .NET Framework and accepts and returns .NET Framework objects. In addition to that it also gives you access to the file system on the computer so that you can access registry,digital signature certificate etc.


Ques: 20. How Do You Comment Out Code In Powershell?
Ans: 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
#>