PowerShell Interview Basic Questions and Answers

How do you find the computer name? PS C:> hostname GOVARDHAN-PC PS C:> $env:COMPUTERNAME GOVARDHAN-PC PS C:> Get-WmiObject win32_operatingsystem | Select-Object -property PSComputerName, CSName PSComputerName                                                      CSName ————–                                                                     —— GOVARDHAN-PC                                                          GOVARDHAN-PC PS C:> Get-ItemProperty -Path “HKLM:SYSTEMCurrentControlSetControlComputerNameActiveComputerName” -Name “ComputerName” | Select-Object -Property Computername ComputerName GOVARDHAN-PC PS C:> How do you find the free space of C: drive in GBs?   PS C:> Get-PSDrive –Name C | ForEach-Object {[math]::round($.free/1GB)} 204 PS C:> Get-PSDrive –Name C |  % {[math]::round($.free/1GB)} 204 PS C:> Get-PSDrive –Name C | Select-Object -Property free | % {[math]::round($_.free/1GB)} 204 PS C:> Get-WMIObject Win32_LogicalDisk -filter “DeviceID=’C:’” | % {[math]::round($_.FreeSpace/1GB)} 204 PS […]

Read more

PowerShell: List only Files and/or Folders

Its very often that need to have a files/folders copied across as part of various automations in systems administration or infrastructure customization.  Copy operations aren’t an issue for smaller sizes and/or less number of files.  But, when you have to copy a large number of files/folders of around GBs in size, it becomes difficult job. While setting file folder copy automation, you need a way to ensure that all the required files and folders or both are successfully to the destination.  To assist that, you can check the count of the files/folder objects in both source and destinations apart from […]

Read more

Basic PowerShell Command-Lets for System Administrators

Find the version of PowerShell you are working in PS C:> $PSVersionTable.PSVersion Major  Minor  Build  Revision —–  —–  —–  ——– 2      0      -1     -1 PS C:> $PSVersionTable | Format-Table -AutoSize Name                      Value —-                      —– CLRVersion                2.0.50727.5466 BuildVersion              6.1.7601.17514 PSVersion                 2.0 WSManStackVersion         2.0 PSCompatibleVersions      {1.0, 2.0} SerializationVersion      1.1.0.1 PSRemotingProtocolVersion 2.1 PS C:> Finding basic system details like computer name, Logged-on domain, system path, system architecture x86 or x64 bit? PS C:> $env:computername GOVARDHAN-PC PS C:> PS C:> $sysinfo = systeminfo PS C:> $lines = $sysinfo -split "n" PS C:> $lines | ? { $_ -match "System Type" } System Type:               x64-based […]

Read more