Quantcast
Channel: Windows PowerShell forum
Viewing all 21975 articles
Browse latest View live

How to get upn without suffix.

$
0
0
How can i get the upn from users in a ou without the suffix?

Out-File - writing a varable to text line by line

$
0
0

Hi All,

Is it possible to use out-file to write a variable into text line by line. Any help is appreciated.

Example Script:

$HotfixList =  (Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Hotfixes\")."Hotfix List"
$HotfixList | Out-File "d:\temp\$date-$CompName-Hotfix.log" -Append


The $HotfixList Variable has output that looks like this at the PowerShell window
PS C:\Windows\system32> $HotfixList
MS16-100,Security Update for Windows Server 2012 R2 (KB3172729)
MS16-111,Security Update for Windows Server 2012 R2 (KB3175024)
MS16-114,Security Update for Windows Server 2012 R2 (KB3177186)
MS16-112,Security Update for Windows Server 2012 R2 (KB3178539)
MS16-115,Security Update for Windows Server 2012 R2 (KB3184943)
MS16-104,Cumulative Security Update for Internet Explorer 11 for Windows Server 2012 R2 (KB3185319)
MS16-106,Security Update for Windows Server 2012 R2 (KB3185911)
MS16-116,Security Update for Windows Server 2012 R2 (KB3184122)
MS14-023,Security Update for Microsoft Office 2013 (KB2880463) 64-Bit Edition
MS14-082,Security Update for Microsoft Office 2013 (KB2726958) 64-Bit Edition
MS15-059,Security Update for Microsoft Office 2013 (KB3039782) 64-Bit Edition
MS15-116,Security Update for Microsoft Access 2013 (KB3085584) 64-Bit Edition
MS15-116,Security Update for Microsoft Project 2013 (KB3101506) 64-Bit Edition
MS15-116,Security Update for Microsoft Publisher 2013 (KB3085561) 64-Bit Edition
MS16-099,Security Update for Microsoft OneNote 2013 (KB3115256) 64-Bit Edition
MS16-099,Security Update for Microsoft Word 2013 (KB3115449) 64-Bit Edition
MS16-099,Security Update for Microsoft Office 2013 (KB3114340) 64-Bit Edition
MS16-097,Security Update for Skype for Business 2015 (KB3115431) 64-Bit Edition
MS16-107,Security Update for Microsoft Office 2013 (KB3118268) 64-Bit Edition
MS16-107,Security Update for Microsoft Outlook 2013 (KB3118280) 64-Bit Edition
MS16-107,Security Update for Microsoft Excel 2013 (KB3118284) 64-Bit Edition
MS16-107,Security Update for Microsoft PowerPoint 2013 (KB3115487) 64-Bit Edition
PS C:\Windows\system32>

But when I look at my text file it looks like the following. I assume I need to use regex of something to format the data.
nstalled HotFix: MS16-100,Security Update for Windows Server 2012 R2 (KB3172729) MS16-111,Security Update for Windows Server 2012 R2 (KB3175024) MS16-114,Security Update for Windows Server 2012 R2 (KB3177186) MS16-112,Security Update for Windows Server 2012 R2 (KB3178539) MS16-115,Security Update for Windows Server 2012 R2 (KB3184943) MS16-104,Cumulative Security Update for Internet Explorer 11 for Windows Server 2012 R2 (KB3185319) MS16-106,Security Update for Windows Server 2012 R2 (KB3185911) MS16-116,Security Update for Windows Server 2012 R2 (KB3184122) MS14-023,Security Update for Microsoft Office 2013 (KB2880463) 64-Bit Edition MS14-082,Security Update for Microsoft Office 2013 (KB2726958) 64-Bit Edition MS15-059,Security Update for Microsoft Office 2013 (KB3039782) 64-Bit Edition MS15-116,Security Update for Microsoft Access 2013 (KB3085584) 64-Bit Edition MS15-116,Security Update for Microsoft Project 2013 (KB3101506) 64-Bit Edition MS15-116,Security Update for Microsoft Publisher 2013 (KB3085561) 64-Bit Edition MS16-099,Security Update for Microsoft OneNote 2013 (KB3115256) 64-Bit Edition MS16-099,Security Update for Microsoft Word 2013 (KB3115449) 64-Bit Edition MS16-099,Security Update for Microsoft Office 2013 (KB3114340) 64-Bit Edition MS16-097,Security Update for Skype for Business 2015 (KB3115431) 64-Bit Edition MS16-107,Security Update for Microsoft Office 2013 (KB3118268) 64-Bit Edition MS16-107,Security Update for Microsoft Outlook 2013 (KB3118280) 64-Bit Edition MS16-107,Security Update for Microsoft Excel 2013 (KB3118284) 64-Bit Edition MS16-107,Security Update for Microsoft PowerPoint 2013 (KB3115487) 64-Bit Edition

thanks in advance
Gaz

PowerShell Eventing without console window open

$
0
0

Hello all,

I am trying to use PowerShell eventing to refresh a webpage every so often, and I thought using PowerShell eventing with a Timer would be a good way to accomplish this. However, it appears I have to keep the PowerShell console window open for the script to work. Is it possible for this script to work without the console window open, i.e. to run as a background task?

cls

$Shell = New-Object -ComObject Shell.application

#First, let's practice by creating a timer object
$Timer = New-Object system.timers.timer

Register-ObjectEvent -InputObject $Timer -EventName Elapsed -MaxTriggerCount 5 -Action {

    #The output from a triggered event action is discarded, so we need to use write-host to capture it
    write-host "Refreshing IE window..." -NoNewline

    #Refresh any "Google" IE windows
    $Shell.Windows() | ?{$_.document.url -like "*Google*"} | %{$_.refresh()}

    write-host "Done" -ForegroundColor Green
}

$Timer.Interval = 1000

$Timer.AutoReset = $true

$Timer.Enabled = $true

Remove heading when using get-adgroup

$
0
0

I'm using Sync-ADObject to replicate a newly created group to all domain controllers before continuing with my script.

The script does

1. Create the group using New-ADGroup

2. Gets the DN of the group using '$repgroup = Get-ADGroup -Identity " " | Select DistinguishedName'

3. In a foreach loop runs against the other Domain Controllers.

I'm getting stuck with step 2 it does bring back the details I require for step 3.  But includes the heading "DistinguishedName and hash line" in the '$repgroup' variable.

How can I stop this from happening?

Disable remote systems network without timeout lag

$
0
0

I am writing a powershell script to quarantine a computer and have a problem of a huge timeout lag when remotely disabling the network interface.

The interface goes offline, but powershell waits ~4-5 minutes to timeout before moving on the the next lines of code.

Anyone know how I could remotely disable the network adapters without the huge time lag?

Thanks in advance!

D

Scheduled reboot with a message

$
0
0

Hi together,

I'd like to realize following:

1) Restart all member computers in a domain group in a scheduled day / time.

2) Scheduled day / time is on Wednesday each 2 weeks.

3) 15 minutes before the server is rebooted, a user group should get an emailthat the computers will be rebooted in 15 minutes.

4) All applications should be forced to close.

5) In each reboot, a log file of the reboot should be generated and saved in a local folder.

6) The scheduled reboot script will be used with a domain group policy.

I can restart the Computers with following script. But how can I realize others?

Get-ADGroupMember "Domain group to restart" |
    ForEach-Object{
        Restart-Computer -ComputerName $_.Name
    }
 

Best Regards

Birdal





PowerShell Casting String to Date

$
0
0

Hello Tommy

Can you also help in converting string value

"13-10-2016 02:33:25"

into DateTime format. The date is 13th Oct 2016, I tried 

$a=[DateTime]::ParseExact("13-10-2016 02:33:25","dd-mm-yyyy hh:mm:ss",$null)

but it does not work and flashed error

"Exception calling "ParseExact" with "3" argument(s): "DateTime pattern 'm' appears more than once with different values."

Thanks in advance for quick help

RP


Fine Grained Password Policy- Custom Email notification

$
0
0

Hi Team,

I am testing FGPP at present is it displaying Password expiry message/Pop-up but the display time is very less not more than 5 seconds and can be easily miss to read.

I am looking out for a script which send automated email informing about password is going to expire in X days.

There are scripts avaialbe but all are for Default Domain level GPO.

can someone please help .

- Atul 


TheAtulA


Change paths of Select-String to Desktop Path

$
0
0

I need to move some scripts and files off a shared drive.

Select-String \\opdrive\Data\*.txt -pattern "Host" -Context 0,1|Out-File \\opdrive\Data\Results\result.txt

Just an example file.  I would like to know how to get both parts to follow desktop path where I have recreated the files and folders.  Both methods I found did not work keep telling me invalid path.  For instance:

Select-String $UserPath=$($env:USERPROFILE)\Desktop\Data\*.txt -pattern "Host" -Context 0,1|Out-File $UserPath=$($env:USERPROFILE)\Desktop\Data\Results\Result.txt

I have read the pages on the special folders but it isn't that clear to me as a newbie where they are used.

Any assistance appreciated.

Move file from Folder A to Folder B based on the name for every 4 seconds

$
0
0

I have requirement to move files from one folder to another (Say folder A to folder B). The file names are in the below format

  • 201610051108266_{93D5B201-B550-4882-9319-9E53CCD91067}
  • 201610051112267_{A5FFD74A-B83A-4A43-B72C-8C93EE8E3592}
  • 201610051112266_{4609651E-43C5-414D-A1FD-CF607926924C}
  • 201610051112266_{6915737B-AD49-49EE-97F2-4DCAEA0B7AAB}

There are around 1000 files in the folder. I need to move files one by one every 4 seconds to another folder and also based on thename. So for the above for files,

First file to be moved from folder A to folder B is

201610051108266_{93D5B201-B550-4882-9319-9E53CCD91067}

After 4 seconds,

201610051112266_{4609651E-43C5-414D-A1FD-CF607926924C}

After 4 seconds,

201610051112266_{6915737B-AD49-49EE-97F2-4DCAEA0B7AAB}

After 4 seconds,

201610051112267_{A5FFD74A-B83A-4A43-B72C-8C93EE8E3592}

Is it possible to do this in Powershell and also please suggest how to implement?

Last boot up time

$
0
0
Is it possible to pull the live last time someone rebooted their computer in powershell using the 
$oulist = 'OU=Computers,OU=BOS,DC=domain,DC=domain1,DC=com','CN=Computers,DC=domain,DC=domain1,DC=com',
'OU=Computers,OU=DC,DC=domain,DC=domain1,DC=com','OU=Computers,OU=DEL,DC=domain,DC=domain1,DC=com',
'OU=Computers,OU=HBG,DC=domain,DC=domain1,DC=com','OU=Computers,OU=NRK,DC=domain,DC=domain1,DC=com',
'OU=Computers,OU=PHL,DC=domain,DC=domain1,DC=com','OU=Computers,OU=PIT,DC=domain,DC=domain1,DC=com',
'OU=Computers IT,OU=PIT,DC=domain,DC=domain1,DC=com','OU=Computers,OU=RIC,DC=domain,DC=domain1,DC=com',
'OU=Computers,OU=TTN,DC=domain,DC=domain1,DC=com','OU=Computers,OU=WNY,DC=domain,DC=domain1,DC=com'

$properties = @(
	'csname',
	@{ N = 'lastbootuptime' ; E = {$_.converttodatetime($_.lastbootuptime)} }
	)
$oulist |
    ForEach {
            Get-WmiObject win32_operatingsystem -ComputerName -searchbase $_ | select $properties

Listing Certificates from Windows 7 command line

$
0
0
How can I list certificates from an other user's personal certificate store on a remote computer?
Is it possible, or not because the certificates are encryped in the personal certificate stores?

Initialize-Disk : The disk has already been initialized.

$
0
0

Hello Everyone

I'm trying to create a bootable usb drive that supports UEFI boot windows 10

I used clear-disk command with -removeData , but the problem is that I need to initialize the USB drive as GPT I receive the following error when I type

$removabledrive = Get-WmiObject Win32_diskdrive | Where-Object interfacetype -EQ usb | Select-Object caption -ExpandProperty caption

Clear-Disk -FriendlyName $removabledrive -RemoveData

Initialize-Disk -FriendlyName $removabledrive –PartitionStyle GPT

any idea how can I do it gpt, my hole purpose is to make uefi bootable device using the mentioned script

thanks for all

Script not hiding from Exchange lists

$
0
0

I'm cleaning up a little script we use at work to help us terminate employees and remove them. The script pulls the user, sets their description, copies all their groups to a text file, deletes them from all the groups, gives them a random password, expires the account, and moves it to a special OU marked for deletion.

All those steps work.. There is a line set to hide the users mailbox from global address lists.. When I run the script, the line doesn't work.. But after the script finishes, if I type the EXACT same line, it does it.. Here's the script with important parts blocked for safety reasons.

Import-Module activedirectory

cls

$today = get-date -Format "MM-dd-yyyy"

$userstoremove = read-host "Enter username "

New-PSDrive -Name dest -Root \\remoteoutputlocation\ -PSProvider FileSystem

foreach ($user in $userstoremove) {

$ranpass = -join ((65..90) + (48..57) + (97..122) | Get-Random -Count 10 | % {[char]$_})

Get-ADPrincipalGroupMembership $user | select name | sort name > "dest:\$user-$today.txt"

# 1. Retrieve the user
$User = Get-ADUser $user -Properties memberOf

# 2. Set user description to delete after today
Set-ADUser $user -Description "DELETE AFTER $today"

# 3. Hide user from address lists

Get-Mailbox -ResultSize "Unlimited" -Identity $user |
Set-Mailbox -HiddenFromAddressListsEnabled $true

# 4. Retrieve groups that the user is a member of
$Groups = $User.memberOf |ForEach-Object {
    Get-ADGroup $_
}

# 5. Go through the groups and remove the user
$Groups |ForEach-Object { Remove-ADGroupMember -Identity $_ -Members $User -Confirm:$false }

# 6. Create a random password
Set-adaccountpassword $user -reset -newpassword (ConvertTo-SecureString -AsPlainText $ranpass -Force)
Write-Host "The password for $user has been set to $ranpass"

# 7. Lets make sure user is expired also
Set-ADAccountExpiration -Identity $user -DateTime '12/10/1980 00:00:00'

# 8. Now to disable the account
Disable-ADAccount -Identity $user

# 9. And move the account to the TO DELETE
Get-ADUser $user | Move-ADObject -TargetPath 'OU=To Delete,DC=somedomain,DC=edu'

As stated, step three doesn't work in the script but if I manually type it after, no issues..

I'm lost here.. What gives?


Script to modify a custom attribute for bulk AD users

$
0
0

Hello,

I'm new to scripting and need to pull from an attribute and place in a new custom attribute.   Specifically I'd like to to pull from Property Name: mail and title E-mail, copy to a custom attribute  mail2 and replace the @domain with a secondary @domain.  I need to do this for users in a specific OU.  

I've tried look for examples and have come close.  Can I do this in one script? Do I need to first copy the attribute and then do a replace or can I do this in one command?

Thanks


Setup a param inside a Function

$
0
0

Hi, this is my first Function, i would like to make $localfilename a dynamic parameter so i can write something like this: 

DonwloadAndShowImage -Localfilename "c:\char1.png"

DonwloadAndShowImage -Localfilename "c:\char2.png"

DonwloadAndShowImage -Localfilename "c:\char3.png"

So i can use the same function to make multiple different files.

function DownloadAndShowImage ($url)
{
    $localfilename = "C:\chart.png"
    $webClient = new-object System.Net.WebClient
    $webClient.Headers.Add("user-agent", "PowerShell xxx")
    $Webclient.DownloadFile($url, $localfilename)
    Invoke-Item $localfilename
}



Rename Computers Using PowerShell and a CSV File

$
0
0

Greetings,

I am about to change over 100 Domain computer names in our organization and would like to do it through Powershell.

I found an script that changes the computer name with csv file but i am not able to get it going :/

function ComputerExists ($sam) { [bool]([adsisearcher]"(sAMAccountName=$sam$)").FindOne() }

$csvfile = "C:\temp\rename.csv"
$csv = Import-Csv $csvfile
    foreach ($c in $csv) {
    $oldName = $c.oldName
    $newName = $c.newName
    if (-not(ComputerExists $newName)) {
        Write-Host "Renaming computer from: $oldName to: $newName"
        netdom renamecomputer $oldName /newName:$newName /uD:domain\user /passwordD:"" /force
        }
    else { Write-Host "$newName already exists" }
    }



I am getting error message 

Renaming computer from:  to: 
The parameter /newName was unexpected.

The syntax of this command is:

NETDOM [ ADD | COMPUTERNAME | HELP | JOIN | MOVE | QUERY | REMOVE |
         MOVENT4BDC | RENAMECOMPUTER | RESET | TRUST | VERIFY | RESETPWD ]


Try "NETDOM HELP" for more information.

I appreciate all help!
Thanks!

 

How to check if action is completed in step 1 and then proceed to step 2 via powershell?

$
0
0

I have a general question about how to handle the flow of powershell commands.

for e.g

1) I am adding a windows server node to an existing cluster, which takes about 60 to 120 seconds per say.

$getclusternode = Get-Cluster –Name "abc" | Add-ClusterNode –Name "node1"

2) then set cluster parameters.  "set-clusterparameters"

could I use something below.

if(!$getclusternode)
    {

    sleep 120
    }


goal is that only after step 1 is completed, then powershell should go to next line to execute the next command in line.  What is the best way to do that?  

PowerShell ISE Freezes after executing WinForms

$
0
0
Hello, I've been having an issue with my PowerShell ISE. I am currently developing a PowerShell script that generates Windows Forms for gathering and displaying data. From what I can tell, the application runs and works great. However, after I execute the code once in the ISE (and close the app/stop execution gracefully); if I leave the ISE idle, for about 10 minutes, the ISE will freeze and I have to crash it with Task Manager. If winform code has not been executed, ISE works fine indefinitely. Thoughts on why my ISE constantly crashes when working on winform based scripts? Thanks.

Run Powershell script as Scheduled task, that uses Excel COM object

$
0
0

What am I missing here..

 

I have  Powershell script that uses the Quest AD cmdlets to get computer information from AD and populate an Excel spreadsheet with the data.

The script works fine, so I created a batch file and started the script from there (which works fine as well). It populates the excel spreadsheet, saves and closes the file.

If I run the script as a scheduled task, I can see from the logging that it supposedly gets the computers from AD, and runs through them. But a file is never saved, I have tried to run the scheduled task with admin credentials.

What am I forgetting?

Viewing all 21975 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>