Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Friday, January 29, 2021

PowerShell Script to repair WMI

 Below PowerShell Script help to Fix the WMI issue.

    Stop-Service Winmgmt -Force -ErrorAction SilentlyContinue

Start-Sleep -Seconds 20


    $status = $null

    Do 

    {

        $status = (Get-Service -Name Winmgmt).Status

    }

    While ($status -notlike "*Stopped*")


Rename-Item -Path C:\Windows\System32\wbem\repository -NewName 'Repository.old1' -Force -ErrorAction SilentlyContinue


Start-Sleep -Seconds 20


Start-Service -Name Winmgmt



Friday, August 28, 2020

How to Create an array with user input in PowerShell


$arr =@() #Create a variable for array
Write-Host "how many numbers you  want in An Array" 
[int]$LenOfArray = Read-Host  #User Input
for ($i=0;$i -lt $LenOfArray; $i++)
{
  $num = Read-Host "Enter $i Index number";
 $arr +=$num
}
Write-Host "The Array is $arr"

Friday, June 5, 2020

Want to hide output in PowerShell ?

Out-Null 

When we execute any command in cmdlet, usually it send its result down the pipe line .
In other words, we can say if we execute any command it display it's output.
Let's see below, here it is listing all the child item inside the windows directory.

 
Now we will use out-null, let's go : Here it does not return any output.

Thursday, May 21, 2020

Using FilterHashTable In powerShell

A hashtable is Similer an array,But it use "key = value” type of syntax.
Let's See how to use FilterHashTable for a filter.

Here is I am trying to get the System event with the specific event ID 1017.
I passed a parameter "Maxevents", will list the only one event .


Get-WinEvent -FilterHashtable @{LogName = 'System'; ID = 1014} -MaxEvents 1|ft -Wrap






Sunday, May 10, 2020

Splatting in PowerShell


Splatting is a method of passing a group  of parameter to a command as unit.
PowerShell associates each value in the collection with a command parameter.
Splatted parameter values are stored in named splatting variables, which look like standard variables, but begin with an At symbol (@) instead of a dollar sign ($).

 The At symbol tells PowerShell that you are passing a collection of values, instead of a single value.
 It Improves the  readability Code.

Here is the way :

$parm = @{
    name = "testuser"
    AccountExpires = "11-05-2020"
    Description ="This is test user,will be deleted soon"
    password = Read-Host "Enter the passoword" -AsSecureString

}
New-LocalUser @parm



Friday, May 8, 2020

PowerShell Passthru Parameter




There are many Windows PowerShell cmdelts that simply work, and they do not return any data.
Here with the Passthru parameter, PowerShell returns the output in the console.

Here I simply use "Start-Process notepad", It started notepad process but did not return the Output
to the console.

PS D:\VS Codes\C\myproject1> Start-Process notepad       
PS D:\VS Codes\C\myproject1>


Now I will use "PassThru" Parameter, Let's See .

PS D:\VS Codes\C\myproject1> Start-Process notepad -PassThru

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
      1       2      520        196       0.16  23436   1 notepad

Here It started the the Process Notepad, and gave me the output of the execution of command .

Note: You want to know how many Windows PowerShell cmdlets have "passthru" patameter

Get-Command -CommandType 8 | where {$_.definition -match ‘passthru’}

Wednesday, May 6, 2020

Out-GridView In PowerShell


The Out-GridView cmdlet sends the output from a command to a grid view window where the output is displayed in an interactive table, which help us to copy the output in tabular format.

Try This:
Get-service|Out-GridView





Wednesday, April 29, 2020

PowerShell Script to Ping Multiple Computers


Ping Multiple computers

This PowerShell Script will get the Computer Name from a Text file, Is located at C:\temp\clients.txt
It will initiated the test connection to all computers and will display the success result.
If any connection gets fail, will store the Failure result on C:\temp\error.txt.

Here is the Script .

$clients=Get-Content C:\temp\clients.txt
foreach ($servers in $clients)
{
try { Test-Connection -ComputerName $servers -Count 1 -ErrorAction Stop  }

catch { $_.exception|Out-File C:\temp\error.txt -Append

}
}

Script View :


Error Log will be as below .

Sunday, December 30, 2018

Export Users From Active Directory

This script will help to Administrator to export the AD users with required Properties.
In this given examples we have Email Address of the the AD users (called as Identity), with the help of this given Identity we will export the users with required properties .



# This is script is used to export the users Properties by any Specific given Identity in Active directory
# Here we have emmail Address of the users, we will export other properties of the users.

$Table = @()

# List of users identity like emailID,UPN,loginID etc

$list= Get-Content C:\Temp\mail.txt
foreach($item in $list){
$user=Get-ADUser -Filter{EmailAddress -like $item} -Properties *
$hitemDetails = New-Object -TypeName psobject -Property @{
username=$user.SamAccountname
Name=$user.GivenName
lastpwset=$user.PasswordLastSet
PW=$user.pwdLastSet
Email=$user.EmailAddress
}
$Table +=$hitemDetails }
# Output as CSV file
$Table|Export-Csv -Path C:\Temp\result.csv





Featured post

System-preferred multifactor authentication (MFA)

Popular Posts