Cyberithub

Top 14 Powershell Interview Questions and Answers

PowerShell language is a high-level proprietary programming syntax developed by Microsoft for the key purpose of enabling system administrators to automate actions and configurations.

The language is based on object-oriented standards but can only be used in Windows environments.

It is part of the .NET framework and typically has C# code underlying its functions, although knowledge of C# is not a prerequisite for learning PowerShell. The closest comparison to the PowerShell language is Perl, which is used in similar scenarios on Linux environments.

Top 14 Powershell Interview Questions and Answers 1

With the PowerShell language, each unique function is referred to as a cmdlet. A cmdlet has one or more sets of defined actions and is capable of returning a .NET object.

Some of the most basic cmdlets that come pre-configured with PowerShell are ones for navigating through a folder structure and moving or copying files.

 

 

Powershell Commands:-

1.Mention what is the command that can be used to get all child folders in a specific folder?

Ans.To get all child folders in a specific folder, you have to use parameter recurse in the code.

C:\>Get-ChildItem C:Scripts –recurse

2.Explain how you can convert the object into HTML?

Ans.To convert the object into HTML

C:\>Get-Process l Sort-object – property CPU –descending l convert to – HTML l Out-file “process.html”

3.What is the code to find the name of the installed application on the current computer?

Ans.Below is the command to find the name of the installed application:-

C:\>Get-WmiObject-Class Win32_Product- ComputerName . l Format-wide-column1

4.Explain how you can find in PowerShell that all the sql services are on one server?

Ans.There are two ways to do this:-

C:\>get-wmiobject win32_service l where-object {$_name-like “*sql*”}
C:\>get-service sql*

5.How to find top 100 processes that are consuming memory more than 100MB and export the list of processes to a CSV file?

Ans. Powershell 2.0

C:\>Get-Process | where-object { $_.pm -gt 100MB }| select -First 100 | Sort pm -Descending

Powershell 3.0

C:\>Get-Process | where pm -gt 100MB | select -First 100 | Sort pm -Descending
c:\>Get-Process | where pm -gt 100MB | select -First 100 | Sort pm -Descending | Export-Csv C:TempProcess-100MB-Memmory.csv
c:\>gci C:TempProcess-100MB-Memmory.csv

6.How to read all log files for “ERROR” and print that lines that matches?

Ans.Below is the command to read all log files:-

C:\>gci “$env:tempCitrixLogs” -File | foreach { Get-Content “$env:tempCitrixLogs$” | foreach { if ($ -match “Error”) {$_} } }

7.How to find all latest events that have “entered the stopped state” in System log within past 24 hours?

Ans.Below is the command to find all latest events:-

C:\>Get-EventLog -LogName System -Message “entered the stopped state” -After $(Get-Date).AddHours(-24) -Newest 5

8.How do you measure the time taken for execution of a PowerShell command?

Ans.Below is the command to measure the time taken for execution:-

C:\> Measure-Command { Get-WmiObject -Class win32_service | where-object { ($.StartMode -eq “Manual”) -and ($.State -eq “Running”) } } | select TotalSeconds | ft -a
C:\>Measure-Command { Get-WmiObject -Class win32_service -Filter “StartMode=’Manual’ and State=’Running'” } | Select TotalSeconds | ft -a
C:\>Measure-Command { Get-WmiObject -Class win32_service -Filter “StartMode=’Manual’ and State=’Running'” } | Select TotalSeconds | ft -a

9.How to send a file as an attachment to an email recipient?

Ans.Below is the command to send a file as an attachment:-

C:\>Send-MailMessage -To myemail@gmail.com -From Dummy@gmail.com -Subject “System Events File is in Attachment” -Attachments “c:tempsysevents.csv” –SmtpServer smtpsrv1.google.com

10.How do you find all services that are set to run “Manual” and not running.

Ans.Below is the command to find all services:-

C:\>Get-WmiObject -Class win32_service -Filter “StartMode=’Manual’ and State!=’Running'” | Select-Object -First 5 | ft -a

11.How do you find the free space of C: drive in GBs?

Ans.Below is the command to find the free space:-

C:> Get-PSDrive –Name C | ForEach-Object {[math]::round($.free/1GB)}

12.How to execute SQL query in PowerShell?

Ans.Below is the command to execute SQL query:-

C:\>Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "MyComputer\MyInstance"

13.How to delete files older than 30 days in c:\temp?

Ans.Below is the command to delete files older than 30 days:-

$Path = "C:\temp"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse ( | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

14.Write a script to start SQL server service on multiple computers.

Ans.First create a .csv file with server and service name:-
servers.csv:-

ComputerName,Service
Server1,Service1
Server2,Service2
$List = Import-CSV c:\scripts\servers.csv
ForEach ($Server in $List)
{ Stop-Service $Server.Service -ComputerName $Server.ComputerName
}
Start-Sleep -Seconds 60
ForEach ($Index in (($List.Count - 1)..0))
{ Start-Service $List[$Index].Service -ComputerName $List[$Index].ComputerName
}

For more information, Please go to this link.

To Know more about Kubernetes command, Please go to this link.

Leave a Comment