How to put a header to csv file generated by Powershell script -


I have got this powerhole script, which has not changed my password for 24 hours. Output from the query has been redirected to the CSV file. Below PowerShell script and batch script:

PowerShell script:

  $ root = [ADSI] '' $ finder = new Object System.DirectoryServices.DirectorySearcher ($ root) $ searcher.filter = "(& amp; (objectCategory = person) (objectClass = user) ((userAccountControl: 1.2.840.113556.1.4.803: = 2)))" $ searcher.sizelimit = 5000 [zero] $ searcher. PropertiesToLoad.Add ( "cn") [zero] $ searcher.PropertiesToLoad.Add ( "sAMAccountName") [zero] $ searcher.PropertiesToLoad.Add ( "pwdLastSet") $ user = $ searcher.findall () $ UserOU = "OU = mountain, DC = Atlanta, DC = singing "$ PWDays = (Get- date) .AddDays (-1) $ UserCount = 0 $ UserPW = 0 foreach ($ user in $ users) {If ($ user.path like "* $ UserOU") {$ UserCount = $ UserCount if ([datetime] :: FromFileTime (($ user.properties.pwdlastset) [0]) le $ PWDays) {$ UserPW = $ UserPW +1 write host $ user.Properties.cn}}}  

batch script:

  powershell.exe d: \ t Emp \ query .ps1 & gt; D: \ temp \ query.csv  

My question is: How do I change the script to keep the header for the username in the csv output file?

The header can be simple 'Username' does not necessarily have the first name and last name.

You are not using export-csv ? You can only pipe your items in it and it will include headers.

  $ something on the lines of users? {$ _. Path-like "* $ UOUUUUUI"} | ? {[Datetime] :: SenderFileTime (($ user.properties.pwdlastset) [0]) -l $ PWDays} | % {$ _ | Add Member-Member-Path Note Authority Username $ _ multiplier.cn} | Choose a username. Export-Csv D: \ temp \ query.csv  

can work (suggestions: pipeline is more fun than loop :))


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -