Solved

Powershell saveas xls directory

Posted on 2010-08-24
2
771 Views
Last Modified: 2012-08-14
I am new to PS and have a few scripts I have put together.  I am running them sequentially from a batch file and all of the scripts use directory paths relative to the batch file.  But, when I have data output to an xls file and attempt to saveas it goes directly into the my documents folder for that user.

I would like it to go into a folder relative to the batch file location.

c:
   powershell
        scripts folder
        log folder
        xls folder
        results folder
        batch file

the powershell script in the script folder will call on log files in the log folder using relative paths but when it comes to saving the xls it does not.  Am I missing something here?

thanks,
C


       
$a = New-Object -comobject Excel.Application 
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$d = $c.UsedRange
$d.EntireColumn.AutoFit()
$b.saveas("Results\SEL.xls") 
$a.quit

Open in new window

0
Comment
Question by:kabaam
2 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 250 total points
ID: 33515429
I do not understand how Excel automation works, it was always a bit of a chaos for me, but here is a workaround (insert it over line 8):

$fs = New-Object -ComObject scripting.filesystemobject
$absolutepath = $fs.GetAbsolutePathName("\Results\SEL.xls")
$b.saveas($absolutepath)
0
 
LVL 11

Author Comment

by:kabaam
ID: 33515529
sweet.  Thanks a bunch.  I removed the first \ in the path and the file saved exactly where I wanted it to go.  I need to remember the getabsolute...

Also, thanks for the quick reply.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question